Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass ENV in docker run command

Is there a way we can pass a variable lets say in this example I want to pass a list of animals into an entrypoint.sh file using ENV animals="turtle, monkey, goose"

But I want to be able to pass different animals when running the container for example docker run -t image animals="mouse,rat,kangaroo"

How do you go about passing arguments when running the docker run command?

The goal is to take that variable when using the docker run command and insert them into that entrypoint.sh file

Right now i hard code that in my Dockerfile. But i want to be able to do this when running the docker run command so I dont always have to change the Dockerfile.

FROM anapsix/alpine-java:8u121b13_jdk

ENV FILE_NAME="file_to_run.zip"

ENV animals="turtle, monkey, goose"

ADD ${FILE_NAME} .

RUN echo "${FILENAME} ${animals}" > ./entrypoint.sh    

CMD [ "/bin/ash", "./entrypoint.sh" ]
like image 405
soniccool Avatar asked Oct 30 '18 20:10

soniccool


People also ask

How do I pass an environment variable in docker run?

When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Can I use env variable in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do you pass an environment variable?

Environment variables can be used to pass configuration to an application when it is run. This is done by adding the definition of the environment variable to the deployment configuration for the application. To add a new environment variable use the oc set env command.

How do I pass a variable in Dockerfile?

Predominantly, there are three different ways through which we can pass environment variables to our Docker containers. These are by using the -e, --env-file, and the ENV instruction inside the Dockerfile.


1 Answers

It looks like you might be confusing the image build with the container run. If the difference between the two isn't immediately clear, I'd recommend reviewing some other questions and docs like:

In Docker, what's the difference between a container and an image? https://docs.docker.com/develop/develop-images/dockerfile_best-practices/


RUN echo "${FILENAME} ${animals}" > ./entrypoint.sh

With the above, the variables will be expanded during the image build. The entrypoint.sh will not contain ${FILENAME} ${animals}. Instead, it will contain

file_to_run.zip turtle, monkey, goose

After the build, the docker run command will create a container from that image and run the above script with the environment variables defined but never used since the script already has the variables expanded. To prevent the variable expansion, you need to escape the $ or use single quotes to prevent the expansion, e.g.

RUN echo "\${FILENAME} \${animals}" > ./entrypoint.sh

or

RUN echo '${FILENAME} ${animals}' > ./entrypoint.sh

I would also recommend being explicit with a #!/bin/ash at the top of this script. Then when you run the script, do not override the command with parameters after the image name. Instead set the environment variables with the appropriate flag to run:

docker run -it -e animals="mouse,rat,kangaroo" image 
like image 128
BMitch Avatar answered Sep 21 '22 14:09

BMitch