Skip to content

Docker and Compose

Use getmyenv with Docker and Docker Compose: wrap docker compose with npx getmyenv run, or write a .env with export for env_file and bind mounts.

Last updated: 2026-09-28

How values reach a container

  • ${VAR} in the compose file, filled in when you run docker compose.
  • A bare environment: - VAR entry, copied from the shell that runs docker compose.
  • env_file:, read from a file.
  • A .env file mounted into the container.
  • docker run -e VAR, copied from the shell.

Wrap the command with run

For ${VAR}, bare environment: entries and docker run -e VAR, wrap the command. Values from run win over --env-file and the project .env for interpolation.

npx getmyenv run production -- docker compose up -d
npx getmyenv run -- docker run -e DATABASE_URL my-image

Write a file with export

env_file: and a mounted .env read a real file. Write it with export, then run your usual command. The file is plaintext and stays on disk until you delete it.

npx getmyenv export -c production -o ./.env --yes
docker compose up -d

What to know

  • After up -d, Docker keeps the values in the container config. docker inspect shows them.
  • docker restart keeps them. Recreating the containers needs run or export again.
  • Keep .env out of images: list it in .dockerignore, and never COPY .env.

Read-only contexts on servers

On a server, set GETMYENV_TOKEN to a server token for the Read-only context. run and export then work from an allowlisted IP, on a trusted or approved machine. Details in /docs/machine-access.