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: env_file with required: false skips a missing file

Docker
~/tips/tip-compose-env-file-required-false

required: false

Lets a compose service load an optional env file that may not exist.

By default a missing env_file is an error, which is right for the file with your real settings. It is a nuisance for a file that only exists on some machines.

The long form of env_file has a required switch:

services:
  app:
    image: example/app:latest
    env_file:
      - path: ./default.env
        required: true
      - path: ./override.env
        required: false

With required: false, Compose silently ignores the entry when the file is not there. When you list several files, the last one wins for a variable that appears twice, and anything under environment: beats all of them. So override.env is a handy place for local tweaks that never go into git, and the stack still starts everywhere else.

Caveat: silently means silently. A typo in the file name is not reported either, and the setting just never arrives. If a value looks wrong, run this and check the environment block:

docker compose config

The .env file next to the compose file is a separate thing. It feeds the ${VAR} substitutions inside the compose file, while env_file entries go into the container’s environment.

Further reading: Compose container spec: env_file

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

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