Andy
Andy

Reputation: 3522

How to get dockerfile to evaluate environment variables I sent to it?

I'm running postgresql inside a docker container. My system comes in two parts, a one time setup, and a runtime. When running the one time setup, I get the user to input a username, password, and dbname, and create a super user and database with those names:

docker run -e USERNAME=user -e PASSWORD=admin -e DBNAME=test myImage

Here's a snippet from the docker file dockerfile:

RUN /etc/init.d/postgresql start && psql --command "CREATE USER $USERNAME WITH SUPERUSER PASSWORD '$PASSWORD';" && createdb -O $USERNAME $DBNAME

I've tested this all with constant user name, password, and database name, but I just can't seem to get it to work when I try to pass environment variables around. I think I read an article earlier which stated that environment variables aren't expanded by the dockerfile, but I've also heard conflicting information that environment variables are the best way to pass this information around in docker right now.

How do I get the above snippet working? Am I just using incorrect syntax to get docker to evaluate the environment variable.

I've also tried inspecting the running container to confirm that the environment variables were set correctly (which they were).

Upvotes: 2

Views: 400

Answers (1)

ateles
ateles

Reputation: 422

I think you misunderstand what RUN does in the Dockerfile; maybe you confuse it with "docker run". Have a look at CMD instead.

Note: the purpose of the Dockerfile is to build an image. The container is a process that is created by the "docker run" command, from an image.

If you want to execute some command when creating a container, you can specify it as an argument to "docker run", as a CMD in the Dockerfile, or as an entrypoint in either place.

Upvotes: 2

Related Questions