Running Docker and replacing variables

I have a variable in a script called image_name

image_name="my-app"

And I'm trying to pass this variable to a docker container, as shown below:

docker build -t $image_name .
docker run --rm --name $image_name -e "app_dir=${image_name}" $image_name

But this variable is not set and is empty in the context of the docker assembly

This is a snippet from the Docker file below:

....

RUN     echo dir is $app_dir

....

The following is a snippet of assembly output:

....

Step 2 : RUN echo dir is $app_dir
---> Running in db93a939d701
dir is
---> c9f5e2a657d5
Removing intermediate container db93a939d701

....

Does anyone know how to make variable substitution?

+4
source share
2 answers

Note that you are using a variable inside Dockerfile. This variable will be evaluated at build time ( docker build), so you need to pass its value at this point. This can be achieved with a parameter --build-arg.

By changing your example, this will be:

docker build --build-arg app_dir=${image_name} -t $image_name .
docker run --rm --name $image_name $image_name
+1

Source: https://habr.com/ru/post/1629891/


All Articles