Sub topic 4
Starting, stopping, and cleaning up with docker compose
This chapter assumes you have a working compose.yaml (such as the shop stack with web and db) and that Docker Engine or Docker Desktop with the docker compose plugin is installed. It covers the commands that move a stack through its lifecycle, and what each one removes.
Start the stack with up
docker compose up reads the Compose file, then creates any missing networks and volumes, builds or pulls images, and creates and starts the containers.
docker compose up -dWithout -d (detached), up stays in the foreground and streams every service's logs. Pressing Ctrl+C then stops the containers. With -d, the command returns once the containers have started, and the stack keeps running in the background.
up is safe to run repeatedly. Compose compares the current configuration with the running containers. It recreates only the services whose configuration or image changed and leaves the rest alone. Two flags override that behavior:
--buildrebuilds images for services that have abuild:section.--force-recreaterecreates containers even if nothing changed.
Add --wait to make up -d block until services are running, or healthy when they define a health check.
Check what is running
docker compose ps
docker compose ps -aps lists the containers of this project with their state and published ports. By default it shows only running containers. -a also shows stopped ones.
Compose groups containers into a project. The project name comes from the top-level name: key (shop in our example). Without it, Compose uses the directory name. You can override it with -p. Container, network, and volume names are prefixed with the project name, such as shop-web-1 and shop_db-data.
Stop, start, and restart
docker compose stop
docker compose start
docker compose restart webstop halts the containers but keeps them, along with the network and volumes. start brings the same containers back. restart stops and starts them again, optionally for a single service. These commands do not re-read the Compose file. After editing it, run up -d instead.
Remove the stack with down
docker compose down
docker compose down -vdown stops and removes the containers and the project's networks. It keeps named volumes, so the data in db-data survives. Adding -v also deletes the named volumes declared in the file. That erases the database.
Other cleanup options:
--rmi localremoves images that Compose built, and--rmi allremoves every image the stack uses.--remove-orphansremoves containers for services you have since deleted from the file.
How the commands relate
Exercise
You run docker compose down and then docker compose up -d. Is the data in the named volume db-data still there? What command would have deleted it?
Show answer
Yes. down removes containers and networks but keeps named volumes, so the new db container mounts the same db-data volume. docker compose down -v would have deleted it.
Exercise
You edit an environment variable in compose.yaml while the stack is running. Should you use restart or up -d, and why?
Show answer
Use up -d. restart reuses the existing containers without re-reading the file. up -d detects the changed configuration and recreates only the affected service.