In this article, we will examine various useful command-line operations in Docker, including how to start, list, stop, and remove containers, and the various options available.
We will cover:
- Prerequisites
- How to start a Docker container
- How to list the running containers
- How to stop a running Docker container
- How to force stop a Docker container
- How to stop all running Docker containers
- Additional options to the Docker stop containers command
- How to verify that the container stopped running
- How to remove a Docker container
- How to stop and remove containers in Docker Compose
- How to stop containers in Docker Swarm
- What is the difference between Docker container stop and kill?
Use docker stop <container_name_or_id>
to gracefully stop a running container. Docker waits 10 seconds by default before forcefully terminating it if it doesn’t exit.
To immediately force-stop a container without waiting, use docker kill <container_name_or_id>
. This is useful in cases where the container does not respond to a graceful stop.
If you don’t already have Docker installed, you can grab it from the official website: Docker Download.
To follow along with this article, once installed, pull the Ubuntu image from the Docker hub so we can use the image with the containers that we will manipulate using the command:
For other Docker commands, check out our Docker cheat sheet.
To run a simple container based on the Ubuntu image we just pulled, run the command below:
docker run -it ubuntu /bin/bash
After running the container, you’ll be inside the container’s shell (in this example, a Bash shell). You can interact with it just like a regular Linux system.
When you’re done, you can exit the container by typing exit
.
Let’s take a look at the command options for docker run
.
This command is used to start a container, and the syntax looks like this:
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
In our example, the IMAGE we used was ubuntu
, the -it
OPTIONS we specified make the container interactive and allocate a terminal. The COMMAND we supplied was the command to run inside the terminal of the running container.
You can find out more about the options available to the Docker run command over on the official Docker reference pages.
To see a list of running containers, you can use the docker ps
command. This command provides information about running containers, including their names, IDs, and status.
docker ps
Learn more: How to List Docker Containers
To stop a running Docker container, use the docker stop
command and provide either the container name or the container ID. You can find these from the information supplied in the docker ps
command.
When you run docker stop
, the main process inside the container will receive SIGTERM
signal, giving the container a chance to shut down gracefully. If the container doesn’t stop within 10 seconds (default timeout), Docker sends SIGKILL
.
The first signal can be changed with the STOPSIGNAL
instruction in the container’s Dockerfile or the --stop-signal
option to docker run
.
Here’s the full syntax of the docker stop
command:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
You can also use the option -s
to change the signal sent to the container, and -t
to supply the number of seconds to wait before killing the container.
For example, as shown above, the container ID of our running Ubuntu container is ccfac1f88d1b:
docker stop ccfac1f88d1b
Run docker ps
again to check that the container is stopped.
To forcefully stop a Docker container immediately without a timeout, you can use the docker kill
command. The main process inside the container is sent the SIGKILL
signal (default), or the signal specified with the --signal
option, which can terminate the process abruptly.
When you run docker kill
, you will notice the container is terminated much quicker than when running docker stop
. This is because the container is not gracefully terminated, and it does not have a chance to perform any cleanup tasks.
The syntax is similar to the docker stop
command:
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Be aware that this can potentially lead to data corruption or unexpected behavior in the container.
Because of this,docker kill
should be used after command with caution, usually after trying docker stop
first to allow the container to shut down cleanly and only resort to docker kill
when the container is unresponsive or cannot be stopped by normal means.
To stop all running Docker containers with a single command rather than individually, you can use a for loop in the shell:
docker stop $(docker ps -q)
This command works by first listing the container IDs of all running containers (docker ps -q
), then passing those IDs to docker stop
, which stops each one. If no containers are running, the command has no effect.
The docker stop
command supports two additional options that control how containers are stopped:
-t
,--time
: Specifies the timeout (in seconds) before forcibly killing the container. By default, Docker waits 10 seconds after sendingSIGTERM
before issuingSIGKILL
. Example:docker stop -t 5 my_container
waits 5 seconds before force-killing.-s
,--signal
: Defines which signal to send instead of the defaultSIGTERM
. This allows customized shutdown behavior if the container handles different signals. Example:docker stop -s SIGINT my_container
.
Fast-growing Brace wanted to streamline its infrastructure processes as its needs evolved. They chose Spacelift for its powerful and growing feature set. As a result, the company has accelerated its deployment processes while auditing–critically important in the financial services world–is now much more straightforward. Spacelift has helped Brace achieve a level of simplicity that makes life much easier for DevOps and developer teams alike.
The docker ps
command shows only currently running containers by default. If your container is not listed there, it’s not running. However, it might still exist in a stopped state. To see both running and stopped containers, use:
docker ps -a
Look for your container in the list. If it appears with a STATUS
like Exited (0)
or Exited (1)
followed by a timestamp, it has stopped.
To remove Docker containers after stopping them, use the docker rm
command followed by the container ID or name.
docker rm <container_id_or_name>
The container must be stopped before removal. If you attempt to remove a running container, Docker will return an error unless you include the -f
(force) flag, which forcibly stops and deletes it.
You can also run it like this:
docker rm $(docker ps -a -q)
The additional -a
option in the docker ps -a -q
command lists all containers, including stopped ones. After running the command, a list of the container IDs that have been removed will be shown on the console:
Similar to the docker kill
command, if required, you can use the docker rm
command with the --kill
option to force the removal of a running container, using SIGKILL
.
Removing all stopped containers
To remove all stopped Docker containers, you can also use:
docker container prune
This command deletes all containers with a Exited
or Created
status. The -f
flag (or --force
) skips the confirmation prompt.
Alternatively, to be more explicit and see the container IDs being removed:
docker rm $(docker ps -a -q -f status=exited)
Both commands only remove stopped containers, not running ones.
In Docker Compose, you can stop and remove containers associated with your Compose project using the docker-compose down
command. You can execute the docker-compose down
command from the directory where your docker-compose.yml
file is located.
Below we have a docker-compose.yaml
file to create a simple Ubuntu container:
version: '3'
services:
my-ubuntu-container:
image: ubuntu
command: sleep infinity
Let’s run docker compose up
to create the container:
And then run docker compose down
to remove it:
This command not only stops the containers but also removes:
- Containers for services defined in the Compose file
- Networks defined in the networks section of the Compose file
- The default network, if one is used
Networks and volumes defined as external are never removed.
To remove named volumes declared in the “volumes” section of the Compose file and anonymous volumes attached to containers, you can use the -v
option.
In Docker Swarm, containers are managed by services, so you cannot stop individual containers directly. Depending on your intent, you can use docker service
or docker node commands
to scale down or remove the service that manages them.
- To reduce the number of running containers to zero:
docker service scale <service_name>=0
- To completely stop and remove all containers associated with the service:
docker service rm <service_name>
- If you want to stop all tasks on a node, to prevent the node from receiving tasks and stop running ones:
docker node update --availability drain <node_name>
Avoid stopping containers manually via docker stop
, as Swarm will restart them to maintain the declared state.
Both commands will terminate containers. The key difference is that docker stop
allows for a graceful shutdown by sending a SIGTERM
signal, while docker kill
forces immediate termination with a SIGKILL
signal.
You should generally prefer docker container stop
when possible to ensure a clean shutdown of your containers, but docker container kill
can be useful when a container is not responding or needs to be stopped forcefully. This distinction mirrors typical Linux signal handling and is important for maintaining data integrity and process stability.
Using a managed CI/CD platform to improve your workflows
We also encourage you to explore the ways Spacelift offers full flexibility when it comes to customizing your workflow. Spacelift is a specialized CI/CD platform for IaC scenarios. It goes above and beyond the support that is offered by the plain backend system. Spacelift enables developer freedom by supporting multiple IaC providers, version control systems, and public cloud endpoints with precise guardrails for universal control.
Instead of manually maintaining build servers, you can simply connect the platform to your repositories. You can then test and apply infrastructure changes directly from your pull requests. It eliminates administration overheads and provides simple self-service developer access within policy-defined guardrails.
You can bring your own Docker image and use it as a runner to speed up deployments that leverage third-party tools. Spacelift’s official runner image can be found here.
If you want to learn more about what you can do with Spacelift, check out this article, create a free account today, or book a demo with one of our engineers.
Manipulating containers on the command line and knowing how to start, stop, kill, and remove containers on demand with the available options is a great way to start learning and experimenting with Docker. Knowing the differences between the stop
and kill
commands, as explained above, is valuable, especially to avoid any issues when running in production.
Solve your infrastructure challenges
Spacelift is a flexible orchestration solution for IaC development. It delivers enhanced collaboration, automation, and controls to simplify and accelerate the provisioning of cloud-based infrastructures.