Skip to content
Go back

Docker Compose useful commands

· Updated:
By SumGuy 5 min read
Docker Compose useful commands

Docker Compose is awesome!

docker compose up

The docker compose up command is used to start the containers defined in the YAML file. If the containers do not exist, they will be created. If the containers already exist, they will be started. if they are already started, it will just do nothing, unless something was edited in the docker-compose.yml file, in which case it will recreate the container.

docker compose up

This command will start all the containers defined in the YAML file. If you want to start a specific container, you can use the following command:

docker-compose up <container-name>

docker compose down

The docker compose down command is used to stop and remove all the containers defined in the YAML file. you can’t bring down a single container, it has to be all of them in the YAML file.

docker-compose down

docker compose logs

The docker compose logs command is used to view the logs of the containers defined in the YAML file.

docker compose logs

This command will display the logs of all the containers defined in the YAML file. If you want to view the logs of a specific container, you can use the following command:

$ docker compose logs -f <container-name>

docker compose ps

The docker-compose ps command is used to list all the containers defined in the YAML file and their status.

docker compose ps

this will give an output like this :

NAME COMMAND SERVICE STATUS PORTS
mycoolapp "/init" mycoolapp running 80/tcp, 3000->3000/tcp

docker compose build

The docker compose build command is used to build the images of the containers defined in the YAML file.

docker compose build

docker compose exec

The docker compose exec command is used to execute a command inside a running container.

docker compose exec mycoolapp somecommand

This command will execute the somecommand inside the mycoolapp container.

e.g. to get to bash and run arbitrary commands inside you mycoolapp container:

docker compose exec mycoolapp bash

docker compose restart

The docker compose restart command is used to restart the containers defined in the YAML file.

docker compose restart

do you need more explanation on any of these? let me know in the comments!

The flags that actually matter

That base command list gets you pretty far, but the flags are where you stop doing things the hard way.

For up, -d (detached mode) is the obvious one everyone learns day one. But --build is the one you forget exists until you’ve spent 20 minutes wondering why your code changes aren’t showing up:

Terminal window
# Start detached, force rebuild images before starting
docker compose up -d --build
# Only restart containers whose config or image changed
docker compose up -d --no-recreate
# Pull fresh images before starting (great for keeping things current)
docker compose up -d --pull always

For down, the default just stops and removes containers. Add --volumes if you actually want to nuke the data too — but for the love of your weekend, don’t run that on a production database without knowing what you’re doing:

Terminal window
# Remove containers + their named volumes (data gone, you've been warned)
docker compose down --volumes
# Remove containers + orphan containers from old service definitions
docker compose down --remove-orphans

That --remove-orphans one is underrated. If you renamed a service in your Compose file, Docker will happily keep running the old container under the old name forever while the new one also runs. --remove-orphans cleans up containers that no longer have a matching service definition. You’ll thank yourself later.

Common gotchas

“Why isn’t my config change taking effect?” — Compose caches the last-run config. If you edited docker-compose.yml but the container was already running, docker compose up -d will recreate only the changed services. If it’s not recreating, double-check that the change is actually in the right file (yes, this has happened to all of us).

“Where are my logs going?” — The default docker compose logs gives you everything from container start. That can be a wall of noise on a long-running service. Use --tail to get just the recent stuff:

Terminal window
# Last 50 lines, then follow new output
docker compose logs --tail=50 -f mycoolapp

“The container says it’s running but the app isn’t responding”docker compose ps shows container status, not application health. A container in running state just means the process started, not that your app is actually ready. If you want real health checks, that’s a healthcheck: block in your Compose file — topic for another day.

docker-compose vs docker compose — The old hyphenated docker-compose was a standalone Python binary (v1). The space version docker compose is the v2 plugin baked into the Docker CLI. If you’re on a system where both exist and they’re behaving differently, that’s why. Stick with docker compose (no hyphen) going forward.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Multi-Platform Docker Builds with buildx
Next Post
Docker Network Aliases: The Feature Nobody Uses

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts