Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get environment variables to persist into an imported Docker image

Tags:

docker

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.

like image 421
Gerrat Avatar asked May 27 '14 15:05

Gerrat


People also ask

Do docker containers inherit environment variables?

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.

Can Dockerfile access environment variables?

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.


1 Answers

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/

like image 88
Thomas Uhrig Avatar answered Oct 23 '22 02:10

Thomas Uhrig