Tools · 7 min read
Docker Compose vs docker run
docker run starts one container from the command line. Docker Compose starts a set of containers described in a YAML file. They use the same engine and produce the same containers, so the question is not which is more capable but which is the better way to describe what you want — and once a project has more than one container, or more than a few flags, the answer is almost always Compose.
This guide compares the two, shows how docker run flags map onto a Compose file, and covers networking, volumes, start-up order and when Compose stops being enough.
Try it yourself with the related tool.
Convert a docker run command →Advertisement
docker run: quick and imperative
A command such as docker run -d --name db -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data -p 5432:5432 postgres:16 tells Docker to do one thing right now. It is ideal for trying an image, running a one-off tool, or a quick experiment. The downside is that the configuration lives only in your shell history. Real setups accumulate flags — environment variables, volumes, networks, restart policies, health checks — and long commands are easy to mistype and hard to share.
Compose: declarative and versioned
A compose.yaml file describes services, and docker compose up makes reality match the file. Each docker run flag has a direct equivalent: -p becomes ports, -e becomes environment, -v becomes volumes, --restart becomes restart, --name is the service key. Because the file is committed with the code, a new teammate runs one command and gets the same stack. Changing a setting and running up again recreates only the containers whose configuration changed.
Networking between containers
With plain docker run, containers on the default bridge network cannot reach each other by name; you have to create a network and attach each container to it. Compose creates a network per project automatically, and every service is reachable by its service name. An app container connects to the database at db:5432 — not localhost, which inside a container means the container itself. This one detail causes a large share of "connection refused" errors in first Compose setups.
Volumes and data
Named volumes keep data when containers are removed; bind mounts map a host directory into the container, which is handy for live-reloading source code in development. In Compose both are declared in the file, and named volumes survive docker compose down unless you add -v. Be careful with that flag: it deletes the database data along with the containers.
Start-up order and health checks
depends_on controls the order in which containers start, but by default a dependency counts as started as soon as its process launches, not when it is ready to accept connections. Add a healthcheck to the database service — for PostgreSQL, pg_isready — and use depends_on with condition: service_healthy so the app waits for a database that actually answers. Applications should still retry connections, because dependencies can restart at any time.
Environment and secrets
Compose reads a .env file in the project directory for variable substitution, and env_file loads variables into a container. Keep .env out of version control and commit a .env.example instead. Profiles let optional services, such as an admin UI or a mail catcher, start only when requested, and multiple Compose files can override settings for development, testing and CI.
When Compose is not enough
Compose runs containers on a single host. It is excellent for development, CI, demos and small production deployments on one server. When you need several machines, automatic failover, rolling updates across a cluster or autoscaling, you move to an orchestrator such as Kubernetes or a managed container service. Even then, Compose usually remains the way developers run the stack locally.

