brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: point Docker Compose at another env file to fill the same file with other values

Docker
~/tips/tip-docker-compose-env-file-flag

TAG=1.27-alpine

Run the same Compose file with a different set of variables per environment.

Compose fills ${TAG} style placeholders from your shell and from a file called .env next to the Compose file. With --env-file you point it at another file instead.

# prod.env
TAG=1.27-alpine

# compose.yaml
services:
  web:
    image: nginx:${TAG:-alpine}
docker compose --env-file prod.env config --images
docker compose --env-file prod.env up -d

The first command prints the image names after substitution, so you can see that the production tag was picked up before anything starts.

This is easy to mix up with the env_file: attribute in a service. That one passes variables into the container. The --env-file flag (and the default .env) only feeds the substitution in the Compose file itself; nothing reaches the container unless the file uses the variable somewhere. I find it helps to think of them as values for the file and values for the app.

A variable that is already set in your shell beats the one in the file. So if TAG is exported from an earlier test, your env file looks ignored. Running docker compose config shows what really won. The option goes before the subcommand, as in the examples above.

Further reading: Set environment variables within your container's environment, Variable interpolation

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link