0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker Compose Stop Command Effectively

Use the docker compose stop command to gracefully stop running containers defined in your Docker Compose file without removing them. You can stop all containers or specify one or more service names to stop only those containers.
📐

Syntax

The basic syntax of the docker compose stop command is:

  • docker compose stop: Stops all running containers defined in the Compose file.
  • docker compose stop [SERVICE...]: Stops only the specified service containers.

This command stops containers gracefully by sending a stop signal, but it does not remove them.

bash
docker compose stop [SERVICE...]
💻

Example

This example shows how to stop all running containers and how to stop a specific service container using Docker Compose.

bash
version: '3.8'
services:
  web:
    image: nginx
  db:
    image: postgres

# Start containers
$ docker compose up -d

# Stop all containers
$ docker compose stop

# Stop only the web service container
$ docker compose stop web
Output
$ docker compose up -d [+] Running 2/2 $ docker compose stop Stopping myproject_web_1 ... done Stopping myproject_db_1 ... done $ docker compose stop web Stopping myproject_web_1 ... done
⚠️

Common Pitfalls

Common mistakes when using docker compose stop include:

  • Expecting containers to be removed: stop only stops containers; use docker compose down to remove them.
  • Not specifying service names when you want to stop only some containers.
  • Confusing stop with kill, where kill forces immediate termination.
bash
## Wrong: expecting containers to be removed
$ docker compose stop
# Containers stop but still exist

## Right: remove containers after stopping
$ docker compose down
📊

Quick Reference

CommandDescription
docker compose stopStops all running containers defined in the Compose file.
docker compose stop web dbStops only the 'web' and 'db' service containers.
docker compose downStops and removes containers, networks, and volumes.
docker compose killForces immediate stop of containers without graceful shutdown.

Key Takeaways

Use docker compose stop to gracefully stop containers without removing them.
Specify service names to stop only certain containers.
Stopped containers remain on your system until removed with docker compose down.
Do not confuse stop with kill; kill forces immediate termination.
Always check running containers with docker compose ps before and after stopping.