{"id":336420,"date":"2021-09-08T14:50:29","date_gmt":"2021-09-08T11:50:29","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/"},"modified":"2021-09-08T14:50:29","modified_gmt":"2021-09-08T11:50:29","slug":"how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/","title":{"rendered":"#How to Pass Environment Variables to Docker Containers \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a42ca06094a5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a42ca06094a5\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/#What_Are_Environment_Variables_Used_For\" >What Are Environment Variables Used For?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/#With_a_Command_Line_Argument\" >With a Command Line Argument<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/#Additional_Security_With_an_env_File\" >Additional Security With an .env File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/#With_Docker-Compose\" >With Docker-Compose<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-pass-environment-variables-to-docker-containers-cloudsavvy-it\/#With_Kubernetes\" >With Kubernetes<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Pass Environment Variables to Docker Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-503\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Programs often control operation through configuration bundled with the software, and environment variables allow users to set them at runtime. However, running processes in Docker containers complicates things, so how do you pass environment variables to a container?<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Are_Environment_Variables_Used_For\"><\/span>What Are Environment Variables Used For?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Environment variables allow you to decouple the configuration from the <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication\u2019s executable.\u00a0For example, you wouldn\u2019t want to store your production database password in your codebase\u2014if you did, it would be visible from Git, and anyone with access to your code could take down your database.<\/p>\n<p>Instead, you set it with an environment variable, which stores a simple key-value pair, and allows you to access the value in\u00a0any application running in the same shell session (they\u2019re not globally accessible).\u00a0 This also has the benefit of being able to easily define different configuration for different environments. For example, having separate keys for development and production databases, or using a different API endpoint.<\/p>\n<p>Setting these variables for Docker containers can be done in three main ways\u2014with CLI arguments, <code>.env<\/code> config files, or through <code>docker-compose<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"With_a_Command_Line_Argument\"><\/span>With a Command Line Argument<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The command used to launch Docker containers, <code>docker run<\/code>, accepts ENV variables as arguments. Simply run it with the <code>-e<\/code>\u00a0flag, shorthand for <code>--env<\/code>, and pass in the key=value pair:<\/p>\n<pre>sudo docker run &#13;\n-e POSTGRES_USER='postgres' &#13;\n-e POSTGRES_PASSWORD='password' &#13;\n...<\/pre>\n<p>And, if you already have those environment variables set in the environment that is running that command, you can just pass them in directly by name:<\/p>\n<pre>\/\/ set variable&#13;\nPOSTGRES_PASSWORD='password'&#13;\n&#13;\n\/\/ use it later&#13;\ndocker run\u00a0-e POSTGRES_PASSWORD -e\u00a0POSTGRES_USER ...<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Additional_Security_With_an_env_File\"><\/span>Additional Security With an .env File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Passing variables with CLI arguments works great, but it has a downside\u2014those variables are visible from the host. They\u2019re logged in the command history, and visible in the process listing for the launched process.<\/p>\n<p>Linux has a built in way to manage permissions for this\u2014file access. Storing the variables in an\u00a0<code>.env<\/code> file allows you to control access to that file with file permissions (<code>chmod<\/code>, <code>chown<\/code>).<\/p>\n<p>Create an <code>.env<\/code>\u00a0file with variables in the following format, each on a new line:<\/p>\n<pre>POSTGRES_PASSWORD='password'&#13;\nPOSTGRES_USER='postgres'&#13;\nAPPLICATION_URL='example.com'<\/pre>\n<p>Then, pass it to <code>docker run<\/code>\u00a0with the <code>--env-file<\/code>\u00a0flag:<\/p>\n<pre>docker run --env-file .\/envfile ...<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"With_Docker-Compose\"><\/span>With Docker-Compose<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, many people do not launch Docker containers directly with <code>docker run<\/code>, and instead opt to use a <code>docker-compose<\/code>\u00a0file to handle the configuration of multiple containers all representing a single application.<\/p>\n<p>To pass environment variables to a container launched this way, you will have to configure the compose file to pass the session\u2019s variables through to the Docker container. This configuration here passes the <code>POSTGRES_USER<\/code>\u00a0variable to both the build environment and the runtime environment, and sets a default value if it does not exist.<\/p>\n<pre>version: '3.1'&#13;\nservices:&#13;\n  my-service: &#13;\n    build:&#13;\n      context: .&#13;\n      args:&#13;\n        - POSTGRES_USER=${POSTGRES_USER:-default}&#13;\n    environment: &#13;\n      - POSTGRES_USER=${POSTGRES_USER:-default}<\/pre>\n<p>You will need to set the environment variables before running <code>docker-compose up<\/code>, otherwise it will not be able to access them. You could store them in the compose file, but that\u2019s usually tracked and versioned, which defeats the purpose of env variables.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"With_Kubernetes\"><\/span>With Kubernetes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes is an orchestration system that can handle running hundreds of containers across a network. It still uses Docker, but you will only ever touch configuration, so passing environment variables directly won\u2019t work.<\/p>\n<p>Instead, you can define them in the configuration for the Pod:<\/p>\n<pre>apiVersion: v1&#13;\nkind: Pod&#13;\nmetadata:&#13;\n  name: example&#13;\nspec:&#13;\n  containers:&#13;\n    - ...&#13;\n      env:&#13;\n        - name: SERVICE_PORT&#13;\n          value: \"80\"&#13;\n        - name: SERVICE_IP&#13;\n          value: \"172.17.0.1\"<\/pre>\n<p>Kubernetes is complicated, and there are a lot of different ways to work with environment variables. To learn more, you can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tasks\/inject-data-application\/_print\/#pg-eff97c25c917cdb414eda016df0e2bca\">read their guides on injecting data into Pods<\/a>.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14081\/how-to-pass-environment-variables-to-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Pass Environment Variables to Docker Containers \u2013 CloudSavvy IT&#8221; Programs often control operation through configuration bundled with the software, and environment variables allow users to set them at runtime. However, running processes in Docker containers complicates things, so how do you pass environment variables to a container? What Are Environment Variables Used For?&#8230;<\/p>\n","protected":false},"author":1,"featured_media":336421,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-336420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/336420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=336420"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/336420\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/336421"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=336420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=336420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=336420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}