This is a long, in-depth read. Let’s start with something you can use right now, without having to read the whole thing!
Here’s a list of easy takeaways:
- The .env file, is only used during a pre-processing step when working with docker-compose.yml files. Dollar-notation variables like $HI are substituted for values contained in an “.env” named file in the same directory.
- 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). You can use ARG values to set ENV values to work around that.
- ENV values are available to containers, but also RUN-style commands during the Docker build starting with the line where they are introduced.
- If you set an environment variable in an intermediate container using bash (RUN export VARI=5 && …) it will not persist in the next command. There’s a way to work around that.
- An env_file, is a convenient way to pass many environment variables to a single command in one batch. This should not be confused with a .env file.
- Setting ARG and ENV values leaves traces in the Docker image. Don’t use them for secrets which are not meant to stick around (well, you kinda can with multi-stage builds).
An Overview
Alright, let’s get started with the details. The guide is split up into the following topics:
- The Dot-Env File (.env)
- ARG and ENV Availability
- Setting ARG Values
- Setting ENV Values
- Overriding ENV Values
Feel free to jump right to the one you need right now. However, you’ll get the best result if you read through them all thoroughly.