Pass .env variables to a Dockerfile with Docker Compose

Pass Compose .env values to Dockerfile build arguments, inspect the resolved configuration, and set runtime environment variables.

Docker Compose can read values from a project’s .env file and substitute them into compose.yaml. To make a value available during an image build, pass it through build.args and declare it with ARG in the Dockerfile. To make a value available when a container starts, set it under the service’s environment or env_file.

This example uses one value to select a base image, one to write a label into a file during the build, and one to configure the running container. It uses the docker compose command and omits the obsolete top-level version field. Docker Compose interpolation · Compose file version

1. Define the values in .env

Create an empty example directory and put this .env file inside it. Run the commands below from that directory.

BASE_IMAGE=alpine:3
BUILD_LABEL=local-demo
APP_MODE=development

These values are safe to print. An exported shell variable with the same name takes precedence over the value in .env. Compose also accepts --env-file to select a different interpolation file. Docker Compose interpolation sources

2. Map the values in compose.yaml

services:
  app:
    build:
      context: .
      args:
        BASE_IMAGE: "${BASE_IMAGE:?Set BASE_IMAGE in .env}"
        BUILD_LABEL: "${BUILD_LABEL:-dev}"
    environment:
      APP_MODE: "${APP_MODE:-development}"

Compose resolves these expressions before invoking the builder. It stops with an error if BASE_IMAGE is missing or empty, and uses dev if BUILD_LABEL is missing or empty. The environment mapping passes APP_MODE to the container at runtime. Putting a value in .env supplies interpolation input. Use an explicit mapping when you want the container to receive it. Compose build arguments · Container environment variables

Add a .dockerignore file so the build context excludes local environment files.

.env
.env.*

3. Declare the arguments in the Dockerfile

ARG BASE_IMAGE=alpine:3
FROM ${BASE_IMAGE}

ARG BASE_IMAGE
ARG BUILD_LABEL=dev
LABEL com.example.base-image="${BASE_IMAGE}"
RUN printf '%s\n' "$BUILD_LABEL" > /build-label

The first ARG BASE_IMAGE is in scope for FROM. The second declaration brings that value into the build stage so LABEL can use it. If you only need a variable in FROM, you can leave out the second declaration. BUILD_LABEL is declared inside the stage because the RUN instruction uses it there. Dockerfile ARG scope

Check the result

Inspect what Compose resolved before building.

docker compose config

Under build.args, look for BASE_IMAGE: alpine:3 and BUILD_LABEL: local-demo. Under environment, look for APP_MODE: development. To check the effect of a shell override, run BUILD_LABEL=ci docker compose config. Docker Compose config command

Build the image and read the build-time file and runtime variable.

docker compose build app
docker compose run --rm app sh -c 'cat /build-label; printenv APP_MODE'

With the sample values and no shell overrides, expect these two lines.

local-demo
development

If you change BUILD_LABEL, build the image again to rewrite /build-label. If you change APP_MODE, start a new container to apply it.

Choose the setting that matches the job

Need Use
Substitute a value into compose.yaml Project .env, the shell, or --env-file
Customize an image build Compose build.args and Dockerfile ARG
Set a variable when a container starts Service environment or env_file
Supply a credential temporarily during a build A BuildKit secret mount

Use build arguments for values you are comfortable exposing in build metadata. Use a secret mount for credentials required during the build, and service secrets for credentials that the running application needs. Docker build variables · Build secrets · Compose secrets