Article
6 Min Read

How To Stop Docker Containers: Full Guide with Examples

Follow these steps to stop a running Docker container. This guide describes the process and provides everything you need to know.

How To Stop Docker Containers: Full Guide with Examples
Listen to this article
13:47
/
19:15
1x

As a software engineer, one phrase you'll often hear is, "It doesn't work on my computer." This is a problem usually caused by incompatible software versions, which many DevOps teams fix using Docker.

Docker is an open-source platform based on Kubernetes (an open-source system for automating deployment, scaling, and managing containerized applications). It's an operating system virtualization technology that allows applications to be packaged with all the necessary dependencies to run on any operating system. Docker achieves this by using a Docker hub as well as containers that hold everything applications need to run smoothly.

To create a Docker container, you need a Docker image that contains specific instructions for packaged applications and preconfigured server environments. A Dockerfile contains these configurations. To run Docker containers on operating systems that don't natively support Docker (like Windows and Mac), Docker offers Docker Desktop (for current systems) and Toolbox (for legacy systems). Linux has native support for Docker. You can also create a Docker Swarm by combining a variety of machines into a cluster.

Docker also provides the Docker daemon tool, which manages Docker images, containers, networks, and volumes. The tool is best used with JSON.

Docker containers can be created, deleted, started, or stopped. This tutorial explains how to stop a running docker container and delete a container.

Understanding Docker and its components

Docker is a platform that helps you build, deploy, and manage applications on different operating systems. When creating software, developers typically have to account for different factors to make their application compatible with multiple systems, which adds complexity to the process.

Docker solves this problem by packaging applications in isolated environments where they have the dependencies and libraries needed to run. So you don't have to worry whether specific plugins your application uses are installed on the host machine. As a result, an application will perform consistently regardless of where it's installed.

When using Docker, you'll interact with the following terms and components:

  • Images. This is a read-only template that specifies how a Docker container should be created. For instance, it lists the libraries and dependencies an application needs to run, facilitating the creation of a suitable operating environment.
  • Containers. These are specific environments that contain application logic, required libraries, and dependencies. Containers allow applications to run independently without relying on the host machine's dependencies.
  • Dockerfile. Docker relies on a set of instructions to build the necessary images. All the commands needed for creating an image are contained in a text document known as the Dockerfile.
  • Docker engine. The Docker engine allows users to build and manage application containers. It contains critical components like the Docker daemon for managing docker objects and API requests.

Docker and virtual machines are both popular technologies for deploying and managing applications. While they have similar functionalities, they also have some differences.

For starters, Docker allows applications to operate in isolated containers but still share the host computer's storage and computational resources. On the other hand, virtual machines offer increased isolation by allowing applications to run on separate instances of an operating system, which enhances security.

Docker also has quicker startup times since it uses the host system's already-running resources. The boot time is longer for virtual machines because they have to create new operating system instances. Regarding portability, Docker containers can be shipped and deployed in different environments quickly. Virtual machines are also portable, but the installation process is more complex and requires the installation of extra dependencies.

Why and when to stop Docker containers

With Docker containers, you can deploy your app in multiple environments without worrying about whether the host system has the required libraries. Docker also provides tools to manage your applications to ensure they work as required.

However, there are instances where you would want to stop Docker containers, including:

  • Maintenance. You may need to stop Docker containers to perform routine maintenance. Doing so allows you to push patches and updates safely without negatively impacting running processes.
  • Troubleshooting. Software doesn't always work as intended. In such cases, you may have to stop Docker containers to troubleshoot and implement corrective measures.
  • Resource management. While Docker containers facilitate an isolated environment, the entire Docker architecture still runs on a host machine—meaning it's limited by the available resources. So, you may have to stop Docker containers occasionally to free up disk space and RAM, or to organize or debug the CPU.
  • Security. In the unfortunate event of a cyberattack, you may have to stop Docker containers quickly to minimize the impact.

Make sure you stop Docker containers properly. Otherwise, you may consume crucial computer resources in the background and slow down performance or affect data integrity and cause some of your data to become corrupt or inaccessible. If the container is part of a microservice architecture, not properly stopping it may cause your entire system to go offline.

Graceful shutdown vs. forced termination

You can stop running Docker containers through a graceful shutdown or forced termination.

Graceful termination involves giving the container time to clean up or complete different tasks before shutdown. On the other hand, forced termination entails stopping a Docker container immediately after the command is issued.

Forced termination is executed using the docker kill command. But a graceful shutdown is done using the docker stop command, followed by the desired grace period. For example, this docker stop --time=30 a0c59618bf9e allows a Docker container to continue running for 30 seconds before being shut down.

Forced termination is useful in situations where application containers become unresponsive. Developers can also execute forced termination during testing to assess the application's performance. Other use cases include when the container contributes to system inability or affects other microservices.

In the next section, we outline detailed steps and tips to help you stop Docker containers properly.

Preparing to stop Docker containers

Before stopping Docker containers, ensure your container is up and running. We provide tips to help you prepare well to stop Docker containers.

Prerequisites: Docker installation and running a container

1. Open your command line or terminal.

For Mac:

For Mac

For Windows:

For Windows

2. You can start a container using the following syntax:

--CODE language-markup--
docker run [ OPTIONS ] IMAGE[:TAG] [COMMAND] [ARG...]


For example, to start a container with these details:

  • Image: centos
  • Name: centos_labs
  • Command: /bin/bash
  • ID: f4h2n5hvwvb

Run the following command:

--CODE language-markup--
docker run -i -t --name=centos_labs centos:latest /bin/bas


Or

--CODE language-markup--
docker start f4h2n5hvwvb


We’ll use a container with the following details for this tutorial:

  • Image: nginx
  • Name: reach-docs
  • Command: /docker-entrypoint
  • ID: a0c59618bf9e

3. Enter the following command for details on the containers currently running:

--CODE language-markup--
docker ps

All containers

The docker ps command gets only the containers currently running, while the docker ps -a command returns all the containers whether running or stopped.

Note: The docker ps command only gets the containers currently running, while the docker ps -a command returns all the containers, whether they are running or stopped.

Important considerations before stopping a container

Before stopping a container, copy its unique ID to avoid confusing it with others. If there are multiple containers, identify and copy the ID or name of the container that you want to stop using Ctrl + C on a Windows keyboard or Command + C on a Mac keyboard.

We will stop the container with ID a0c59618bf9e in the next section.

How to stop Docker containers using docker stop

You can make one docker container stop running or stop all containers simultaneously. This section will show you how to stop one running Docker container and all running Docker containers.

How to stop one Docker container at a time

Use the following syntax to stop a docker container from running:

--CODE language-markup--
docker stop container


In the syntax above, “container” is the docker container’s ID. Execute the command below to stop the container with ID a0c59618bf9e.

--CODE language-markup--
docker stop a0c59618bf9e

Container step 2

That stops the container with ID a0c59618bf9e from running. To confirm it was successful, check the list of containers currently running.

Container step 3

Recall that when we ran the docker ps command the first time, we had one container running. Now, we have none after stopping it from running.

You can also check by running the docker ps -a command.

Container step 4

How to stop containers gracefully to prevent data loss

By default, the docker stop command gives a grace period of 10 seconds for the running docker container to exit. The docker container is stopped forcefully after the grace period.

However, you can choose to allocate a different grace period. It might be necessary to do this when a container needs more grace periods to stop running. To do that, use the following syntax:

--CODE language-markup--
docker stop [-t|--time[=10]] CONTAINER


In the syntax above, you can choose to use –time or -t. Both work fine.

For example, to stop the container with ID a0c59618bf9e from running with a grace period of 30 seconds, use the following command: 9618bf9e.

--CODE language-markup--
docker stop --time=30 a0c59618bf9e


You can use the docker kill command if you don’t want to give a grace period before stopping a running docker container. So, instead of using docker stop a0c59618bf9e, run docker kill a0c59618bf9e.

How “docker stop” works

The docker stop command sends out two signals before terminating a running container. The first signal warns the container of possible termination after a certain grace period. It’s called the SIGTERM signal. The second signal is the SIGKILL signal that terminates the running container.

How “docker kill” works

The docker kill command sends out only one signal to a running container, the SIGKILL signal that terminates the running container. This command takes a shorter period to execute since it gives no grace period.

Stopping multiple containers at once

The following steps show you how to stop all containers at once.

1. Let’s begin by starting many Docker containers or just starting all of your Docker containers. To start all of the containers at once, run the following command:

--CODE language-markup--
docker start $(docker ps -a -q)

Stop all 1

Confirm that it worked by running docker ps -a.

Stop all 2

Notice the status. It shows how long each container has been running. In our case, all the containers have been running for up to two minutes.

2. Run the following command to stop all the running containers at once:

--CODE language-markup--
docker stop `docker ps -q`

Stop all 3

Use the docker ps -a command to view all containers and confirm that none of them are running.

Stop all 4

That is how to stop all running containers at once. You can now stop running containers as you deem fit.

How to delete a Docker container

Generally, you’ll use the docker rm command to delete Docker containers. Like stopping a running container, you can also delete one or more containers at once.

Delete one Docker container

The following steps explain how to delete a container.

1. Let’s begin by starting many Docker containers or just starting all of your Docker containers. To start all of the containers at once, run the following command:

Delete step 1

2. Use the following syntax to delete a container:

--CODE language-markup--
docker rm [ OPTIONS ] CONTAINER [ CONTAINER ]


For example, to delete the container with ID a0c59618bf9e, run the command:

--CODE language-markup--
docker rm a0c59618bf9e

Delete step 2

Using flags with the docker stop command

A container might still refuse to be deleted even after being stopped from running, or you might need to delete the container while it’s still running. In that situation, you can apply force by adding the -f flag to the command. Use the following command instead:

--CODE language-markup--
docker rm -f a0c59618bf9e

Delete all Docker containers

Follow the steps below to delete all containers at once.

1. Stop all the containers using this command:

--CODE language-markup--
docker stop `docker ps -q`

Delete all containers 1

2. Use the following command to delete all the containers:

--CODE language-markup--
docker rm `docker ps -aq`

Delete all containers 2

Deleting a container or multiple containers can be that quick!

Listing Docker containers

Run the following command to get a list of all of your running docker containers:

--CODE language-markup--
docker ps

Listing Docker containers 1

Run the following command to get a list of all of your containers, whether running or stopped:

--CODE language-markup--
docker ps -a

Listing Docker containers 2

The list comes in a tabular form with the following columns:

  1. Container ID is a unique identifier that differentiates one container from the others. It consists of alphabets and numbers.
  2. Image is the file used to execute code in a Docker container.
  3. Command provides directives on how to start a container. The default command is /bin/bash unless you specify something else.
  4. Created states the date of the container’s creation.
  5. Status indicates whether the container is running or not.
  6. Ports shows the port occupied by a running container. This column is usually empty when the container isn’t running.
  7. Name is the random label given to the container by the Docker daemon at the point of creation. You may also specify your container’s name.

For an introduction to Docker, refer to our Docker basics.

Graceful shutdown vs forced termination

You can stop running Docker containers through a graceful shutdown or forced termination.

Graceful termination involves giving the container time to clean up or complete different tasks before shutdown. On the other hand, forced termination entails stopping a Docker container immediately after the command is issued.

Forced termination is executed using the docker kill command. But a graceful shutdown is done using the docker stop command, followed by the desired grace period. For example, this docker stop --time=30 a0c59618bf9e allows a Docker container to continue running for 30 seconds before being shut down.

Forced termination is useful in situations where application containers become unresponsive. Developers can also execute forced termination during testing to assess the application's performance. Other use cases include when the container contributes to system inability or affects other microservices.

Advanced techniques and tips

Manually stopping containers can be monotonous. A good solution is to automate container stoppage with scripts. For instance, you can schedule a script to execute when a container surpasses its resource allocation. However, be cautious and only stop the intended container to avoid service downtime.

You should also consider timeouts for stopping containers. In this context, timeout is the duration a Docker container will continue running after a stop command and before being gracefully shut down. Using timeouts can help preserve data integrity by giving running processes enough time to complete data handling operations.

Incorrectly stopping Docker containers can lead to consequences like higher consumption of CPU resources and data loss. Common issues likely to affect container stoppage include unresponsive containers, long-running network operations, and container configuration.

If your container becomes unresponsive, use the docker kill command to forcefully terminate it. You should also investigate whether any background file system and network processes are blocking container termination and stop them. Also, search for scripts in your Docker environment that may cause your containers to behave unexpectedly.

Best practices for managing Docker containers

Docker helps you ship and deploy applications in different environments faster. Tips to help you manage Docker containers more effectively include:

  • Use docker stop over the docker kill command. Apart from isolated cases, use the docker stop command to stop containers. By default, the docker stop command allows containers to shut down gracefully—allowing running processes to detach safely.
  • Reduce container size. Docker containers should only have the required dependencies for your app to run, decreasing its size.
  • Adopt the single responsibility principle. Host your crucial application processes in multiple containers. This lets you know what specific containers do, meaning you can manage them more effectively.
  • Monitor container health. Use different Docker CLI commands to check the performance of containers regularly. Pay attention to factors like resource usage to identify performance-related issues.

Find Docker experts on Upwork

Whether you work alone or in a team, using Docker can help you avoid future software complications or incompatibilities. It can even come in handy if you aren't a DevOps engineer.

Mastering Docker's fundamentals takes time, especially if you're a beginner. You may want professional guidance to help you manage Docker environments. Consider signing up and working with experienced Docker specialists on Upwork. Check out this guide on how to post a job on Upwork to help streamline the process.

If you're an expert looking for work, search Upwork, the world's work marketplace. You can browse the best independent Docker jobs and work on diverse projects to build your portfolio.

Upwork is not affiliated with and does not sponsor or endorse any of the tools or services discussed in this article. These tools and services are provided only as potential options, and each reader and company should take the time needed to adequately analyze and determine the tools or services that would best fit their specific needs and situation.

Heading
asdassdsad
Projects related to this article:
No items found.

Author Spotlight

How To Stop Docker Containers: Full Guide with Examples
The Upwork Team

Upwork is the world’s work marketplace that connects businesses with independent talent from across the globe. We serve everyone from one-person startups to large, Fortune 100 enterprises with a powerful, trust-driven platform that enables companies and talent to work together in new ways that unlock their potential.

Latest articles

Popular articles

Join Upwork, where talent and opportunity connect.