I create a Dockerfile with the following:
FROM ubuntu
ENV test this
I build the image in the current directory:
docker build -t="test" .
I run this image, just to confirm that my environment variable is set:
docker run -t -i <image id> /bin/bash
I type set
within the running container, and confirm that test=this
.
After exiting the container, I export the image with the following two commands:
cont=`docker run -d <image id> ls`
docker export $cont > test.tar
I then import the image via:
cat test.tar | docker import - imported
Run the new image:
docker run -t -i <image id returned from prev cmd> /bin/bash
I type set
within this running container, and my environment variable no longer exists
What am I doing wrong?
I've done this with images with other commands in, and everything but the environment variables seem to persist.
Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.
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. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.
You could save the image instead of export the container:
docker save your/image > /home/you/some-file.tar
If you use save
, you need to use load
to restore the image:
docker load < /home/you/some-file.tar
I wrote a blog post about the difference of export
and save
a couple of weeks ago: http://tuhrig.de/difference-between-save-and-export-in-docker/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With