Home / Alt manpages / docker-stop(1)

  • docker-stop(1)
  • User command
  • linux

Stop Docker Containers Without Losing Their Shutdown Window

You will finish with a safe, repeatable way to stop one or more Docker containers, check that they really stopped, and choose a signal or timeout when the default is not suitable. The examples use the Docker 29.8.1 CLI installed from docker-ce-cli.

Allow about ten minutes for a single container. You need a shell, a container name or ID, and permission to talk to the Docker daemon. On many Linux systems that means membership of the docker group; otherwise prefix the Docker commands with sudo if that is how your host is administered. A command that stops a production service is disruptive, so check the target before you run it.

1. Check the installed command

First confirm the command and the two options this guide uses. This is a read-only check and normally needs no elevated privilege:

$ docker --version
Docker version 29.8.1, build ...
$ docker stop --help
Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Aliases:
  docker container stop, docker stop

Options:
  -s, --signal string   Signal to send to the container
  -t, --timeout int     Seconds to wait before killing the container

The build identifier varies between packages, so do not compare the build ... text literally. The useful checkpoint is that docker stop is available and lists --signal and --timeout. The installed manpage describes docker stop as an alias for docker container stop.

2. Identify the exact container

List running containers before stopping anything. This does not change their state:

$ docker ps --format 'table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}'
CONTAINER ID   NAMES             IMAGE          STATUS
8f2c1a4b6d10   example-web       nginx:latest   Up 2 hours
3c91e7b2a044   example-worker    example:1.4    Up 2 hours

The IDs, names, images and elapsed times are examples. Use the name or ID printed by your own host. Names are easier to review in an incident; IDs are useful when an automation system has already recorded one. Do not copy a row from this guide and assume it exists on your machine.

Checkpoint: make sure the target is the service you intend to interrupt. If you need to inspect labels, mounts or the configured stop behaviour, use a read-only command such as docker inspect CONTAINER_NAME before proceeding.

3. Stop one container gracefully

Run the simplest form with the verified name:

$ docker stop example-web
example-web

Docker sends the container's configured stop signal to its main process and waits for the stop timeout. If the process has not exited when that period ends, Docker forcibly kills it. The normal signal is usually SIGTERM, but the image or container can specify a different stop signal. The command prints the container name when it succeeds.

This is a state-changing operation. It stops the container but does not remove it, its image, or its volumes. To undo the stopped state, start the same container again:

$ docker start example-web
example-web

Starting a container is not always equivalent to resuming a service exactly where it was. Applications may need to replay work or reconnect to dependencies, so use the service's own recovery procedure when one exists.

4. Verify the result

Ask Docker for the container's state after the stop command returns:

$ docker ps --filter name=example-web --format '{{.Names}} {{.Status}}'
$ docker ps -a --filter name=example-web --format '{{.Names}} {{.Status}}'
example-web Exited (0) 4 seconds ago

The first command only shows running containers, so an empty first result is expected for a stopped target. The second includes stopped containers and should show an Exited status. The exit code depends on how the application's main process finished; it is not automatically evidence that the application completed all work successfully.

If you need the recorded exit code without relying on formatted output, use:

$ docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}}' example-web
exited exit=0

5. Stop several containers together

The command accepts more than one container argument. Review every target first, then stop the group:

$ docker ps --format '{{.Names}}'
example-web
example-worker
$ docker stop example-web example-worker
example-web
example-worker

Each name printed by docker stop is a target Docker processed. Verify the group afterwards:

$ docker ps -a --filter name=example-web --filter name=example-worker \
    --format '{{.Names}} {{.Status}}'
example-web Exited (0) 8 seconds ago
example-worker Exited (0) 7 seconds ago

Keep explicit names in a one-off command. A broad shell expansion or an unreviewed list from docker ps -q can stop unrelated workloads. If an orchestrator owns these containers, make the change through that orchestrator or it may start them again.

6. Adjust the signal only when the application expects it

Use --signal when the container's main process deliberately handles another signal:

$ docker stop --signal=SIGINT example-worker
example-worker

The local CLI accepts a signal value, while the Docker reference documents signal names such as SIGKILL and numeric signal values. Prefer the name that matches the application's documentation. A signal is delivered to the container's main process, and shell-form entrypoints can prevent a child process from receiving it directly. Check the image's entrypoint and the application's signal handling if a graceful stop is not happening.

Do not use SIGKILL as a routine shortcut. It removes the application's chance to flush data, close files, or finish a transaction. If you have already sent a custom signal and the container remains running, inspect its state and logs before escalating.

7. Set a finite timeout for a known slow shutdown

Use --timeout when this particular stop needs a different waiting period:

$ docker stop --timeout=30 example-worker
example-worker

The value is seconds. Docker sends the configured stop signal, waits up to 30 seconds, then uses SIGKILL if the container is still running. A shorter value reduces the maintenance window but raises the chance of an abrupt kill. A longer value protects a slow shutdown but delays recovery if the process is stuck.

The container's configured stop timeout is used when you omit this option. Docker's daemon supplies a default when neither the container nor the command has one. On Linux, the documented daemon default is 10 seconds; Windows containers use 30 seconds. Treat that as a fallback, not as a promise that every workload is safe to stop within ten seconds.

The special value --timeout=-1 makes Docker wait indefinitely. Use it only when an operator is actively watching the shutdown and an unbounded wait is acceptable. Otherwise a finite timeout gives you a clear checkpoint and a predictable failure boundary.

8. Diagnose the common failures

If Docker says the container does not exist, list both running and stopped containers and check spelling:

$ docker ps -a --format '{{.ID}} {{.Names}} {{.Status}}'
$ docker inspect example-web

If the daemon cannot be reached, check the Docker service and the socket access using your host's normal administration process. Do not add sudo blindly: it can select a different Docker configuration or context from your unprivileged shell.

If the command waits until the timeout and then returns, the application did not stop within the available window. Read its logs, inspect the main process and confirm whether data was left mid-operation:

$ docker logs --tail 100 example-worker
$ docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}}' example-worker

Do not immediately repeat the stop command with SIGKILL just to make the prompt return. A forced kill may be the correct incident action, but it is a destructive escalation and should follow the service's recovery plan.

Done means

  • You checked the installed Docker CLI version and confirmed the stop syntax.
  • You identified the exact container name or ID before changing state.
  • You used the normal graceful stop path unless the application required another signal.
  • You verified the result with docker ps -a or docker inspect.
  • You know that stopping does not remove the container, and you have a safe docker start recovery path.
  • You treated timeout expiry and SIGKILL as escalation points, not harmless defaults.