{"id":994,"date":"2016-11-01T08:30:19","date_gmt":"2016-11-01T13:30:19","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=994"},"modified":"2016-11-01T08:30:19","modified_gmt":"2016-11-01T13:30:19","slug":"docker-test-environment-variable","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/docker-test-environment-variable\/","title":{"rendered":"Docker Test Environment Variable"},"content":{"rendered":"
I did a simple test of how to utilize environment variables in docker images and recored a few notes here.<\/p>\n
<\/p>\n
Install Docker on a Ubuntu 16.04.1 Virtualbox guest for this test.<\/strong> Simple docker test.<\/strong><\/p>\n Fix docker permissions so normal user can use.<\/strong><\/p>\n Make sure docker runs at OS boot time.<\/strong><\/p>\n Test simple python web app.<\/strong> Try an environment variable.<\/strong><\/p>\n Browser response:<\/strong>\u00a0http:\/\/192.168.1.134:32770\/ Make a change to app.py. Play with restart\/commit etc to understand volatile vs non-volatile changes.<\/strong><\/p>\n Check changes in browser:<\/strong> http:\/\/192.168.1.134:32773\/ NOTES\/IDEAS:<\/strong><\/p>\n http:\/\/stackoverflow.com\/questions\/30494050\/how-to-pass-environment-variables-to-docker-containers<\/p>\n Question: Answer: I did a simple test of how to utilize environment variables in docker images and recored a few notes here.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-994","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=994"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/994\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\nhttps:\/\/docs.docker.com\/engine\/installation\/linux\/ubuntulinux\/<\/p>\n\r\nroot@docker:~# apt-key adv --keyserver hkp:\/\/p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D\r\n\r\nroot@docker:~# echo "deb https:\/\/apt.dockerproject.org\/repo ubuntu-xenial main" | sudo tee \/etc\/apt\/sources.list.d\/docker.list\r\ndeb https:\/\/apt.dockerproject.org\/repo ubuntu-xenial main\r\n\r\nroot@docker:~# apt-get update\r\nroot@docker:~# apt-cache policy docker-engine\r\n\r\nroot@docker:~# apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual\r\n<\/pre>\n
\r\nroot@docker:~# docker run hello-world\r\nUnable to find image 'hello-world:latest' locally\r\nlatest: Pulling from library\/hello-world\r\nc04b14da8d14: Pull complete \r\nDigest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9\r\nStatus: Downloaded newer image for hello-world:latest\r\n\r\nHello from Docker!\r\nThis message shows that your installation appears to be working correctly.\r\n\r\nTo generate this message, Docker took the following steps:\r\n 1. The Docker client contacted the Docker daemon.\r\n 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.\r\n 3. The Docker daemon created a new container from that image which runs the\r\n executable that produces the output you are currently reading.\r\n 4. The Docker daemon streamed that output to the Docker client, which sent it\r\n to your terminal.\r\n\r\nTo try something more ambitious, you can run an Ubuntu container with:\r\n $ docker run -it ubuntu bash\r\n\r\nShare images, automate workflows, and more with a free Docker Hub account:\r\n https:\/\/hub.docker.com\r\n\r\nFor more examples and ideas, visit:\r\n https:\/\/docs.docker.com\/engine\/userguide\/\r\n<\/pre>\n
\r\nroot@docker:~# groupadd docker\r\ngroupadd: group 'docker' already exists\r\nroot@docker:~# usermod -aG docker rrosso\r\n\r\nrrosso@docker:~$ docker run hello-world\r\n\r\nHello from Docker!\r\nThis message shows that your installation appears to be working correctly.\r\n\r\nTo generate this message, Docker took the following steps:\r\n 1. The Docker client contacted the Docker daemon.\r\n 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.\r\n 3. The Docker daemon created a new container from that image which runs the\r\n executable that produces the output you are currently reading.\r\n 4. The Docker daemon streamed that output to the Docker client, which sent it\r\n to your terminal.\r\n\r\nTo try something more ambitious, you can run an Ubuntu container with:\r\n $ docker run -it ubuntu bash\r\n\r\nShare images, automate workflows, and more with a free Docker Hub account:\r\n https:\/\/hub.docker.com\r\n\r\nFor more examples and ideas, visit:\r\n https:\/\/docs.docker.com\/engine\/userguide\/\r\n<\/pre>\n
\r\nroot@docker:~# systemctl enable docker\r\nSynchronizing state of docker.service with SysV init with \/lib\/systemd\/systemd-sysv-install...\r\nExecuting \/lib\/systemd\/systemd-sysv-install enable docker\r\n<\/pre>\n
\nhttps:\/\/docs.docker.com\/engine\/tutorials\/usingdocker\/<\/p>\n\r\nrrosso@docker:~$ docker run -d -P training\/webapp python app.py\r\nUnable to find image 'training\/webapp:latest' locally\r\nlatest: Pulling from training\/webapp\r\ne190868d63f8: Pull complete \r\n909cd34c6fd7: Pull complete \r\n0b9bfabab7c1: Pull complete \r\na3ed95caeb02: Pull complete \r\n10bbbc0fc0ff: Pull complete \r\nfca59b508e9f: Pull complete \r\ne7ae2541b15b: Pull complete \r\n9dd97ef58ce9: Pull complete \r\na4c1b0cb7af7: Pull complete \r\nDigest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d\r\nStatus: Downloaded newer image for training\/webapp:latest\r\n331df8667f005e40555944b7e61108525e843b6262275808f016695aacd7fc67\r\n\r\nrrosso@docker:~$ docker ps -l\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\n331df8667f00 training\/webapp "python app.py" 34 seconds ago Up 33 seconds 0.0.0.0:32768->5000\/tcp focused_jang\r\n\r\nrrosso@docker:~$ docker inspect focused_jang\r\n[\r\n {\r\n "Id": "331df8667f005e40555944b7e61108525e843b6262275808f016695aacd7fc67",\r\n "Created": "2016-10-31T21:18:32.596139908Z",\r\n "Path": "python",\r\n "Args": [\r\n "app.py"\r\n ],\r\n "State": {\r\n "Status": "running",\r\n[.. snip ..]\r\n "Gateway": "172.17.0.1",\r\n "IPAddress": "172.17.0.2",\r\n "IPPrefixLen": 16,\r\n "IPv6Gateway": "",\r\n "GlobalIPv6Address": "",\r\n "GlobalIPv6PrefixLen": 0,\r\n "MacAddress": "02:42:ac:11:00:02"\r\n }\r\n }\r\n }\r\n }\r\n]\r\n\r\nrrosso@docker:~$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' focused_jang\r\n172.17.0.2\r\n<\/pre>\n
\r\nrrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training\/webapp python app.py\r\nc300d87ef0a9ad3ca5f3c40fd5ae7d54f095ada52d1f092e991a2271652b573b\r\nrrosso@docker:~$ docker ps -l\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\nc300d87ef0a9 training\/webapp "python app.py" 11 seconds ago Up 10 seconds 0.0.0.0:32770->5000\/tcp sleepy_varahamihira\r\n<\/pre>\n
\nHello app1!<\/p>\n \r\nrrosso@docker:~$ docker stop sleepy_varahamihira\r\nsleepy_varahamihira\r\nrrosso@docker:~$ docker run -t -i training\/webapp \/bin\/bash\r\nroot@3c94c01cc795:\/opt\/webapp# vi app.py \r\nroot@3c94c01cc795:\/opt\/webapp# cat app.py \r\nimport os\r\n\r\nfrom flask import Flask\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello():\r\n provider = str(os.environ.get('PROVIDER', 'world'))\r\n return 'Riaan added '+provider+'!'\r\n\r\nif __name__ == '__main__':\r\n # Bind to PORT if defined, otherwise default to 5000.\r\n port = int(os.environ.get('PORT', 5000))\r\n app.run(host='0.0.0.0', port=port)\r\nroot@3c94c01cc795:\/opt\/webapp# exit\r\n\r\nrrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training\/webapp python app.py\r\n8116836ea65f7254e81671a58b68cf263b42427b066a1c3cfc971c70303b614d\r\n\r\nrrosso@docker:~$ docker stop evil_colden\r\nevil_colden\r\n\r\nrrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training\/webapp python app.py\r\n190207e1eadad71205256a5127de982ad837dbad5a41896546ba480e2416ba20\r\n\r\nrrosso@docker:~$ docker ps -l\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\n190207e1eada training\/webapp "python app.py" 8 seconds ago Up 7 seconds 0.0.0.0:32772->5000\/tcp condescending_wilson\r\n\r\nrrosso@docker:~$ docker stop condescending_wilson\r\ncondescending_wilson\r\n\r\nrrosso@docker:~$ docker run -t -i training\/webapp \/bin\/bash\r\nroot@f6dd40e4a838:\/opt\/webapp# vi app.py \r\nroot@f6dd40e4a838:\/opt\/webapp# exit\r\n\r\nrrosso@docker:~$ docker ps -l\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\nf6dd40e4a838 training\/webapp "\/bin\/bash" About a minute ago Exited (0) 5 seconds ago determined_cray\r\n\r\nrrosso@docker:~$ docker commit f6dd40e4a838 training\/webapp\r\nsha256:8392632ac934525ae846d6a2e0284a52de7cbfa1a682bf8a9804966c5c3e15c9\r\n\r\nrrosso@docker:~$ docker run -e "PROVIDER=app1" -d -P training\/webapp python app.py\r\n594d6edb3ab7400ae3c38584c0d48276f24973a4328c3f60c1b1bc4cce16f17a\r\n\r\nrrosso@docker:~$ docker ps -l\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\n594d6edb3ab7 training\/webapp "python app.py" 5 seconds ago Up 4 seconds 0.0.0.0:32773->5000\/tcp focused_hawking\r\n<\/pre>\n
\nRiaan added app1!<\/p>\n
\n# Dockerfile
\nENV DATABASE_URL amazon:rds\/connection?string<\/p>\n
\nYou can pass environment variables to your containers with the -e flag.
\nexample from a startup script<\/p>\n\r\nsudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \\ \r\n-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \\\r\n-e POSTGRES_ENV_POSTGRES_USER='bar' \\\r\n-e POSTGRES_ENV_DB_NAME='mysite_staging' \\\r\n-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \\\r\n-e SITE_URL='staging.mysite.com' \\\r\n-p 80:80 \\\r\n--link redis:redis \\ \r\n--name container_name dockerhub_id\/image_name\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"