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: docker run --env-file loads environment variables from a file

Docker
~/tips/tip-docker-run-env-file

DB_HOST=192.168.1.10

Reads container environment variables from a file instead of a long row of -e flags.

Once a docker run line has four or five -e flags, I stop being able to read it. Moving them into a file keeps the command short and puts the values in one place.

The format from the docs is simple: one VAR=value per line, a line with only VAR takes the value from your own environment, and lines that start with # are comments. A # anywhere else is part of the value.

# app.env
# database settings
DB_HOST=192.168.1.10
DB_USER=app
TZ=Europe/Amsterdam
DB_PASSWORD
docker run -d --name app --env-file ./app.env example/app:1.0

The last line of the file has no value, so Docker copies DB_PASSWORD from the shell you run the command in. That lets you keep the secret out of the file, and out of anything you commit.

Because a # in the middle of a line belongs to the value, never put a trailing comment after a value. The file is also plain text on disk, so if it holds real passwords, keep it out of git and set its permissions with chmod 600.

Further reading: docker run reference: env-file

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