I did a simple test of how to utilize environment variables in docker images and recored a few notes here.
Install Docker on a Ubuntu 16.04.1 Virtualbox guest for this test.
https://docs.docker.com/engine/installation/linux/ubuntulinux/
root@docker:~# apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
root@docker:~# echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.listdeb https://apt.dockerproject.org/repo ubuntu-xenial main
root@docker:~# apt-get updateroot@docker:~# apt-cache policy docker-engine
root@docker:~# apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtualSimple docker test.
root@docker:~# docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldc04b14da8d14: Pull completeDigest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9Status: Downloaded newer image for hello-world:latest
Hello from Docker!This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com
For more examples and ideas, visit: https://docs.docker.com/engine/userguide/Fix docker permissions so normal user can use.
root@docker:~# groupadd dockergroupadd: group 'docker' already existsroot@docker:~# usermod -aG docker rrosso
rrosso@docker:~$ docker run hello-world
Hello from Docker!This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com
For more examples and ideas, visit: https://docs.docker.com/engine/userguide/Make sure docker runs at OS boot time.
root@docker:~# systemctl enable dockerSynchronizing state of docker.service with SysV init with /lib/systemd/systemd-sysv-install...Executing /lib/systemd/systemd-sysv-install enable dockerTest simple python web app.
https://docs.docker.com/engine/tutorials/usingdocker/
rrosso@docker:~$ docker run -d -P training/webapp python app.pyUnable to find image 'training/webapp:latest' locallylatest: Pulling from training/webappe190868d63f8: Pull complete909cd34c6fd7: Pull complete0b9bfabab7c1: Pull completea3ed95caeb02: Pull complete10bbbc0fc0ff: Pull completefca59b508e9f: Pull completee7ae2541b15b: Pull complete9dd97ef58ce9: Pull completea4c1b0cb7af7: Pull completeDigest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11dStatus: Downloaded newer image for training/webapp:latest331df8667f005e40555944b7e61108525e843b6262275808f016695aacd7fc67
rrosso@docker:~$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES331df8667f00 training/webapp "python app.py" 34 seconds ago Up 33 seconds 0.0.0.0:32768->5000/tcp focused_jang
rrosso@docker:~$ docker inspect focused_jang[ { "Id": "331df8667f005e40555944b7e61108525e843b6262275808f016695aacd7fc67", "Created": "2016-10-31T21:18:32.596139908Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running",[.. snip ..] "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } }]
rrosso@docker:~$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' focused_jang172.17.0.2Try an environment variable.
rrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training/webapp python app.pyc300d87ef0a9ad3ca5f3c40fd5ae7d54f095ada52d1f092e991a2271652b573brrosso@docker:~$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc300d87ef0a9 training/webapp "python app.py" 11 seconds ago Up 10 seconds 0.0.0.0:32770->5000/tcp sleepy_varahamihiraBrowser response: http://192.168.1.134:32770/
Hello app1!
Make a change to app.py. Play with restart/commit etc to understand volatile vs non-volatile changes.
rrosso@docker:~$ docker stop sleepy_varahamihirasleepy_varahamihirarrosso@docker:~$ docker run -t -i training/webapp /bin/bashroot@3c94c01cc795:/opt/webapp# vi app.pyroot@3c94c01cc795:/opt/webapp# cat app.pyimport os
from flask import Flask
app = Flask(__name__)
@app.route('/')def hello(): provider = str(os.environ.get('PROVIDER', 'world')) return 'Riaan added '+provider+'!'
if __name__ == '__main__': # Bind to PORT if defined, otherwise default to 5000. port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port)root@3c94c01cc795:/opt/webapp# exit
rrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training/webapp python app.py8116836ea65f7254e81671a58b68cf263b42427b066a1c3cfc971c70303b614d
rrosso@docker:~$ docker stop evil_coldenevil_colden
rrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training/webapp python app.py190207e1eadad71205256a5127de982ad837dbad5a41896546ba480e2416ba20
rrosso@docker:~$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES190207e1eada training/webapp "python app.py" 8 seconds ago Up 7 seconds 0.0.0.0:32772->5000/tcp condescending_wilson
rrosso@docker:~$ docker stop condescending_wilsoncondescending_wilson
rrosso@docker:~$ docker run -t -i training/webapp /bin/bashroot@f6dd40e4a838:/opt/webapp# vi app.pyroot@f6dd40e4a838:/opt/webapp# exit
rrosso@docker:~$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf6dd40e4a838 training/webapp "/bin/bash" About a minute ago Exited (0) 5 seconds ago determined_cray
rrosso@docker:~$ docker commit f6dd40e4a838 training/webappsha256:8392632ac934525ae846d6a2e0284a52de7cbfa1a682bf8a9804966c5c3e15c9
rrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training/webapp python app.py594d6edb3ab7400ae3c38584c0d48276f24973a4328c3f60c1b1bc4cce16f17a
rrosso@docker:~$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES594d6edb3ab7 training/webapp "python app.py" 5 seconds ago Up 4 seconds 0.0.0.0:32773->5000/tcp focused_hawkingCheck changes in browser: http://192.168.1.134:32773/
Riaan added app1!
NOTES/IDEAS:
http://stackoverflow.com/questions/30494050/how-to-pass-environment-variables-to-docker-containers
Question:
# Dockerfile
ENV DATABASE_URL amazon:rds/connection?string
Answer:
You can pass environment variables to your containers with the -e flag.
example from a startup script
sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \-e POSTGRES_ENV_POSTGRES_USER='bar' \-e POSTGRES_ENV_DB_NAME='mysite_staging' \-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \-e SITE_URL='staging.mysite.com' \-p 80:80 \--link redis:redis \--name container_name dockerhub_id/image_name