Docker ARG, ENV and .env - a Complete Guide · vsupalov.com - 1 views
-
understand and use Docker build-time variables, environment variables and docker-compose templating the right way.
-
ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT, CMD).
-
ENV values are available to containers, but also RUN-style commands during the Docker build starting with the line where they are introduced.
- ...20 more annotations...
-
set an environment variable in an intermediate container using bash (RUN export VARI=5 && …) it will not persist in the next command.
-
An env_file, is a convenient way to pass many environment variables to a single command in one batch.
-
If you have a file named .env in your project, it’s only used to put values into the docker-compose.yml file which is in the same folder. Those are used with Docker Compose and Docker Stack.
-
Just type docker-compose config. This way you’ll see how the docker-compose.yml file content looks after the substitution step has been performed without running anything else.
-
ARG are also known as build-time variables. They are only available from the moment they are ‘announced’ in the Dockerfile with an ARG instruction up to the moment when the image is built.
-
ENV variables are also available during the build, as soon as you introduce them with an ENV instruction. However, unlike ARG, they are also accessible by containers started from the final image.
-
If you don’t provide a value to expected ARG variables which don’t have a default, you’ll get an error message.
-
The precedence is, from stronger to less-strong: stuff the containerized application sets, values from single environment entries, values from the env_file(s) and finally Dockerfile defaults.