Sub topic 13
Putting it together in one docker run command
This chapter assumes you know how -p publishes a port, how -e and --env-file set variables, and that a run-time -e value overrides an image ENV default. You also need Docker installed and a basic idea of TCP ports and localhost.
The goal
A real service needs both settings at once. It must be reachable on a chosen port, and it must receive its configuration. The official Postgres image is a good example. It refuses to start without POSTGRES_PASSWORD, and it listens on port 5432 inside the container.
One command, three decisions
docker run -d --name db \
-p 127.0.0.1:5432:5432 \
-e POSTGRES_PASSWORD=example \
postgres:16Each part answers one question:
| Part | Decision |
|---|---|
| `-d --name db` | Run in the background under a name you can refer to later |
| `-p 127.0.0.1:5432:5432` | Reach container port 5432 only from this machine, on host port 5432 |
| `-e POSTGRES_PASSWORD=example` | Give the process the variable it needs at startup |
| `postgres:16` | The image, with a pinned tag |
All options go before the image name. Anything after the image name is passed to the container as its command, so -p placed there is silently ignored by Docker and handed to the program inside.
Checking the result
Confirm the container is running and the mapping exists:
docker ps --filter name=db
docker port dbdocker port db should print 5432/tcp -> 127.0.0.1:5432. Then check that the variable reached the process and that the server started:
docker exec db printenv POSTGRES_PASSWORD
docker logs dbThe log should end with a line saying the database system is ready to accept connections. If you have psql installed on the host, psql -h 127.0.0.1 -U postgres connects through the published port.
Moving settings into a file
Once a command carries several variables, move them into an env file. This keeps the command short and the values out of your shell history.
cat > db.env <<'EOF'
# database settings
POSTGRES_PASSWORD=example
POSTGRES_DB=appdb
EOF
docker run -d --name db2 \
-p 127.0.0.1:5433:5432 \
--env-file ./db.env \
postgres:16Here the host port is 5433 and the container port stays 5432. Use a different host port when 5432 is already taken, such as by the first db container. The container side never changes, because the server inside still listens on 5432.
Common mistakes in combined commands
- Options after the image name. Docker treats them as arguments to the container's command, so the port is never published.
- Host port already in use. Docker reports that the port is already allocated. Stop the other container or pick a new host port.
- Dropping the host IP by accident. Writing
-p 5432:5432binds all interfaces, so other machines on the network can reach the database. Keep127.0.0.1:unless you need remote access. - Missing required variables. Postgres exits right away without
POSTGRES_PASSWORD. Rundocker logs dbto see the reason. - Variables changed after first start. The Postgres image only reads
POSTGRES_PASSWORDwhen it creates a new data directory. Changing-eon a container that already has data does not change the stored password.
Cleaning up
Remove both containers when you finish:
docker rm -f db db2Exercise
You run docker run -d --name cache redis -p 127.0.0.1:6379:6379 -e MODE=test. The container starts, but redis-cli -h 127.0.0.1 cannot connect. What is wrong, and how do you fix it?
Show answer
The -p and -e options come after the image name redis, so Docker passes them to the container's command instead of reading them as docker run options. No port is published. Move them before the image: docker run -d --name cache -p 127.0.0.1:6379:6379 -e MODE=test redis. Then check the mapping with docker port cache.
Exercise
A container named db already uses host port 5432. Write the options to start a second Postgres container that listens on host port 5433, reachable only from your machine, with variables read from db.env.
Show answer
-p 127.0.0.1:5433:5432 --env-file ./db.env, placed before the image name postgres:16. The host port changes to 5433 and the container port stays 5432.