As a software engineer, one phrase you’ll often hear is, “It doesn’t work on my computer.” Incompatible software versions usually cause this problem. So, many DevOps teams use Docker to fix the issue.
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 the application on any operating system.
Docker achieves this by using containers, which hold everything needed for the smooth running of an application.
To create a Docker container, you need a Docker image that contains specific instructions to 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) Toolbox (for legacy systems). Linux has native support for Docker.
Docker also provides the Docker daemon tool, which manages Docker images, containers, networks, and volumes.
Docker containers can be created, deleted, started, or stopped. This tutorial explains how to stop a running docker container and how to delete a container.
- How to stop a running Docker container
- How to delete a Docker container
- Listing Docker containers
How to stop a running Docker container
You can choose to make one docker container stop running or stop all containers at once. 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
Follow the steps below to make one docker container stop running:
1. Open your command line or terminal.
2. You can start a container using the following syntax:
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:
docker run -i -t --name=centos_labs centos:latest /bin/bas
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:
The docker ps command gets only the containers currently running, while the docker ps -a command returns all the containers whether running or stopped.
4. If there are multiple containers, identify and copy the ID or name of the container that you want to stop (You may use Ctrl + C on your Windows keyboard or Command + C on your Mac keyboard for that). In this case, stop the container with ID a0c59618bf9e.
5. To stop a docker container from running, use the following syntax:
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.
docker stop a0c59618bf9e
That stops the container with ID a0c59618bf9e from running. To confirm it was successful, check the list of containers currently running.
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.
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:
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.
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. It’s the SIGKILL signal that terminates the running container. This command takes a shorter period to execute since it gives no grace period.
How to stop all running Docker containers
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:
docker start $(docker ps -a -q)
Confirm that it worked by running docker ps -a.
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:
docker stop `docker ps -q`
Use the docker ps -a command to view all containers and confirm that none of them are running.
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. Stop the container you intend to delete using the steps outlined in the previous section. It’s necessary to stop the running container; without it, you’ll get an error while trying to delete that container.
2. Use the following syntax to delete a container:
docker rm [ OPTIONS ] CONTAINER [ CONTAINER ]
For example, to delete the container with ID a0c59618bf9e, run the command:
docker rm a0c59618bf9e
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:
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:
docker stop `docker ps -q`
2. Use the following command to delete all the containers:
docker rm `docker ps -aq`
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:
Run the following command to get a list of all of your containers, whether running or stopped:
docker ps -a
The list comes in a tabular form with the following columns:
- Container ID is a unique identifier that differentiates one container from the others. It consists of alphabets and numbers.
- Image is the file used to execute code in a Docker container.
- Command provides directives on how to start a container. The default command is /bin/bash unless you specify something else.
- Created states the date of the container’s creation.
- Status indicates whether the container is running or not.
- Ports shows the port occupied by a running container. This column is usually empty when the container isn’t running.
- 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.
Hire a Docker expert or work as one yourself
Whether you work alone or in a team, it’s a good idea to use Docker to avoid future software complications or incompatibilities. It can come in handy even if you aren’t a DevOps engineer.
Are you looking to hire a Docker expert or work as one yourself? Start with Ndiwano.com, the world’s work marketplace. We make it easy to browse the best independent Docker jobs or hire Docker specialists to help you with your next job.
Leave your comment
You must be logged in to post a comment.