
{"id":5186,"date":"2025-01-19T15:44:50","date_gmt":"2025-01-19T20:44:50","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5186"},"modified":"2025-01-19T15:44:50","modified_gmt":"2025-01-19T20:44:50","slug":"arg-in-docker-actually-creates-an-environment-variable-at-build-time","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5186","title":{"rendered":"ARG in docker actually creates an environment variable, at build time"},"content":{"rendered":"<p>A few days ago I discovered (with the help from my colleagues) that <code>ARG<\/code> instruction in Docker creates a true environment variable that is available inside the commands invoked at build time.<\/p>\n<p>Before, I thought that ARG &#8220;creates a variable&#8221; that exists only in the processor of docker files and is basically a glorified macro. <a href=\"https:\/\/docs.docker.com\/reference\/dockerfile\/#arg\">The documentation<\/a> is somewhat ambiguous about it.<\/p>\n<p>To test, I created a Dockerfile that invokes a command during build time, but does not pass the ARG to it explicitly. Instead, the command accesses the ARG as an environment variable.<\/p>\n<p>See <a href=\"https:\/\/github.com\/ikriv-samples\/docker-arg\">https:\/\/github.com\/ikriv-samples\/docker-arg<\/a>.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM python:3.12-alpine\r\nARG FLAVOR\r\nCOPY init.py .\r\nRUN python init.py\r\nRUN chmod 755 go.sh\r\nCMD sh -c .\/go.sh\r\n<\/pre>\n<p>The <code>init.py<\/code> script:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nwith open(&#039;go.sh&#039;, &#039;w&#039;) as output:\r\n    print(f&quot;echo {os.environ&#x5B;&#039;FLAVOR&#039;]}&quot;, file=output)\r\n<\/pre>\n<p>This creates a `go.sh` script that contains the flavor verbatim, which is executed at the startup of the container.<\/p>\n<h1>ARG vs ENV<\/h1>\n<p>ARG creates an environment variable available only during build time.<br \/>\nENV creates an environment variable available during container run time.<br \/>\nWe can link the two using this command:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nARG FLAVOR\r\nENV FLAVOR=${FLAVOR}\r\n<\/pre>\n<p>This copies the value of the build-time environment variable <code>FLAVOR<\/code> to the run-time environment variable <code>FLAVOR<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago I discovered (with the help from my colleagues) that ARG instruction in Docker creates a true environment variable that is available inside the commands invoked at <a href=\"https:\/\/ikriv.com\/blog\/?p=5186\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-5186","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5186"}],"version-history":[{"count":1,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5186\/revisions"}],"predecessor-version":[{"id":5187,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5186\/revisions\/5187"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}