This repository demonstrates how to work with environment variables in Docker. It covers the following topics:
- Using
.env
files - Setting environment variables from the OS
- Passing environment variables via
Dockerfile
- Using environment variables with
docker-compose.yml
- Building Docker images with build arguments
You can store environment variables in a .env file and make them available to Docker.
docker run --env-file .env --rm my-image
MY_VARIABLE_FROM_OS: None
MY_VARIABLE_FROM_ENV: Hello from .env file
MY_VARIABLE_FROM_DOCKERFILE: Hello from Dockerfile!
MY_VARIABLE_FROM_DC: None
MY_BUILD_ARG (MY_VARIABLE):
MY_BUILD_ARG_FROM_DC (MY_BUILD_ARG_FROM_DC):
The following shows how to make the .env
available in the service myapp
.
version: '3.8'
services:
myapp:
env_file:
- .env
In the section 4. we will see how to run the service and the expected output.
You can pass environment variables from your OS to Docker containers. For example:
export MY_VARIABLE_FROM_OS="Hello, from os!"
docker run -e MY_VARIABLE_FROM_OS --rm my-image
MY_VARIABLE_FROM_OS: Hello, from os!
MY_VARIABLE_FROM_ENV: None
MY_VARIABLE_FROM_DOCKERFILE: Hello from Dockerfile!
MY_VARIABLE_FROM_DC: None
MY_BUILD_ARG (MY_VARIABLE):
MY_BUILD_ARG_FROM_DC (MY_BUILD_ARG_FROM_DC):
You can use the ENV
instruction in a Dockerfile to set environment variables that will be available during runtime. Build the docker image using:
docker build -t my-image .
After building your Docker image, you can run the container and check the output of the environment variables by using the following command:
docker run --rm my-image
MY_VARIABLE_FROM_OS: None
MY_VARIABLE_FROM_ENV: None
MY_VARIABLE_FROM_DOCKERFILE: Hello from Dockerfile!
MY_VARIABLE_FROM_DC: None
MY_BUILD_ARG (MY_VARIABLE):
MY_BUILD_ARG_FROM_DC (MY_BUILD_ARG_FROM_DC):
You can define environment variables directly in your docker-compose.yml file. These variables are passed to the container when it starts.
After setting up your docker-compose.yml and Dockerfile, you can run your service with:
docker-compose up --build
This command will build the Docker image (taking into account any build arguments), start the container, and apply all the environment variables from the .env file, docker-compose.yml, and Dockerfile.
Creating docker-env-vars-example_myapp_1 ... done
Attaching to docker-env-vars-example_myapp_1
myapp_1 | MY_VARIABLE_FROM_OS: None
myapp_1 | MY_VARIABLE_FROM_ENV: None
myapp_1 | MY_VARIABLE_FROM_DOCKERFILE: Hello from Dockerfile!
myapp_1 | MY_VARIABLE_FROM_DC: Hello from docker-compose.yml
myapp_1 | MY_BUILD_ARG (MY_VARIABLE):
myapp_1 | MY_BUILD_ARG_FROM_DC (MY_BUILD_ARG_FROM_DC): Hello build build ARG from docker-compose.yml
You can pass build-time arguments using the ARG
instruction in the Dockerfile.
Build with arguments:
docker build --build-arg MY_BUILD_ARG="Hello during build from command line" -t my-image .
After building your Docker image, you can run the container and check the output of the environment variables by using the following command:
docker run --rm my-image
MY_VARIABLE_FROM_OS: None
MY_VARIABLE_FROM_ENV: None
MY_VARIABLE_FROM_DOCKERFILE: Hello from Dockerfile
MY_VARIABLE_FROM_DC: None
MY_BUILD_ARG (MY_VARIABLE): Hello during build from command line
MY_BUILD_ARG_FROM_DC (MY_BUILD_ARG_FROM_DC):
Build arguments (ARG) are only available during the build process. However, you can pass them to runtime environment variables using ENV if needed.
Distributed under the Apache License. See LICENSE for more information.