Every self-hosting guide starts with a single docker run command, and for a while that is all you need. Then the command grows: ports, two volumes, a restart policy, an environment file, a health check. You keep it in a notes file, paste it with slight differences each time, and one day you cannot remember which flags the running container was started with.
That is the moment to move to Docker Compose. This guide explains what Compose adds, how each common docker run flag translates into compose.yaml, how to convert existing commands automatically, and how to keep passwords out of the file you will eventually commit.
What Compose gives you that docker run does not
docker run is imperative: it starts one container, right now, with the options you typed. Docker Compose is declarative: you describe the containers you want in a YAML file, and docker compose up makes reality match it.
That shift brings practical benefits:
- The configuration is a file. You can review it, put it in version control and share it with a colleague.
- Several containers start together. An app and its database are defined side by side, on a shared network where they reach each other by service name.
- Updates are one command. Change the image tag, run
docker compose up -d, and Compose recreates only what changed. - Clean-up is predictable.
docker compose downstops and removes the containers and the default network while keeping named volumes.
docker run is still the right choice for a quick one-off: testing an image, running a single command in a throwaway container, or a CI step.
How docker run flags map to Compose
docker run flag |
compose.yaml key |
|---|---|
--name db |
container_name: db (the service key is usually enough) |
-p 8080:80 |
ports: ["8080:80"] |
-v data:/var/lib/data |
volumes: on the service, plus a top-level volumes: entry |
-e KEY=value |
environment: |
--env-file ./app.env |
env_file: |
--restart unless-stopped |
restart: unless-stopped |
--network backend |
networks: on the service, plus a top-level networks: entry |
--health-cmd "..." |
healthcheck: test: ["CMD-SHELL", "..."] |
--label key=value |
labels: |
--memory 512m / --cpus 1.5 |
mem_limit: / cpus: |
| arguments after the image | command: |
A few flags have no equivalent. --rm is unnecessary because Compose removes containers on down. -P (publish all exposed ports) must be replaced by an explicit ports: list. -d is not stored in the file; you pass it to docker compose up -d instead.
Convert your commands automatically
The Docker Run to Compose converter parses the commands in your browser, so nothing is sent to a server.
- Open Docker Run to Compose and paste one or more
docker runcommands into docker run command(s), use Open file for a.shor.txtscript, or click Load sample. - Multi-line commands work: line continuations with
\(bash), a backtick (PowerShell) or^(Windows cmd) are joined automatically. - Read the compose.yaml output. Each command becomes a service, named after
--nameor, if there is none, the image name. - Read the Review these before running box. It lists flags that were skipped or need manual work, such as
--rm,-P,--network-aliasor unknown options. - Copy the YAML or Download it as
compose.yaml, then start it withdocker compose up -d.
For example, these two commands:
docker run -d --name reports-db -p 127.0.0.1:5432:5432 \
--env-file ./db.env -v reports-data:/var/lib/postgresql/data \
--network backend --restart unless-stopped postgres:16-alpine
docker run -d --name reports-api -p 8080:8080 \
--env-file ./api.env --network backend example/reports-api:1.4
become a Compose file along these lines:
services:
reports-db:
image: "postgres:16-alpine"
container_name: reports-db
restart: unless-stopped
ports:
- "127.0.0.1:5432:5432"
volumes:
- "reports-data:/var/lib/postgresql/data"
env_file:
- "./db.env"
networks:
- backend
reports-api:
image: "example/reports-api:1.4"
container_name: reports-api
ports:
- "8080:8080"
env_file:
- "./api.env"
networks:
- backend
volumes:
reports-data:
networks:
backend:
external: true
Named volumes are declared at the top level automatically. Custom networks are marked external: true, because docker run --network backend implies the network already exists. If you want Compose to create and own it, delete the external: true line.
Keep secrets out of the Compose file
Many docker run examples pass passwords with -e POSTGRES_PASSWORD=.... Converting that literally puts the password in compose.yaml, and Compose files tend to end up in Git. Do this instead:
- Move sensitive values into an env file, such as
db.env, and reference it withenv_file:. - Add the env file to
.gitignorebefore your first commit, and commit adb.env.examplewith placeholder values so others know which variables to set. The .gitignore Generator includes an Environment files (.env) template that ignores.envfiles while keeping.env.example. - Restrict the file's permissions on the server, for example
chmod 600 db.env; the Chmod Calculator explains the numbers. - For stronger isolation, use Compose's
secrets:feature, which mounts a secret as a file inside the container; many official images accept a_FILEvariant of their password variable for this purpose.
Also consider binding database ports to 127.0.0.1 as in the example above, so the database is reachable only from the host and other containers, not from the whole network.
Other ways to write a Compose file
- By hand from the documentation. Many images publish an example
compose.yaml; starting from it is often better than converting an old command, because it reflects the maintainers' recommended settings. - From a running container.
docker inspect <name>shows every setting a container was started with, which helps when the original command is lost. It is verbose, so pick out only what you set deliberately. - Editing the YAML format. If you need the same configuration as JSON for another tool, YAML to JSON converts it.
Troubleshooting
"network backend declared as external, but could not be found." Create it once with docker network create backend, or remove external: true so Compose creates it.
The app cannot reach the database on localhost. Inside a container, localhost is the container itself. Use the service name (reports-db) as the host name.
A port like 22:22 behaves strangely. YAML can misread unquoted xx:yy values as numbers in base 60. The converter quotes ports for this reason; keep them quoted when editing.
Data disappeared after docker compose down -v. The -v flag deletes named volumes. Use plain down unless you really want to wipe the data.
Frequently asked questions
Is Docker Compose better than docker run?
For anything you run more than once, usually yes, because the configuration lives in a reviewable file. For quick experiments and one-off commands, docker run remains simpler.
Should the file be called docker-compose.yml or compose.yaml?
Current Docker Compose prefers compose.yaml but still reads docker-compose.yml. Use one name consistently in a project.
Can I put passwords directly in compose.yaml?
Avoid it. Keep secrets in an env file that is excluded from Git, or use Compose secrets, and commit only an example file with placeholders.
Why was a flag missing from the converted file?
Some flags have no Compose equivalent or are not needed, such as --rm, -d and -P. The converter lists every skipped flag under Review these before running.
Do I need docker-compose or docker compose?
Modern Docker includes Compose as a plugin, run as docker compose. The older standalone docker-compose binary is legacy and no longer the recommended way.