Learn How To Stop, Kill And Clean Up Docker Containers

0
12709
docker-containers

docker-containers

In the article ‘learn how to create and start Docker containers’ we discussed the docker run, create and start commands. We discussed how to customize properties of a container. We will build on the concepts covered in that tutorial in this tutorial. This tutorial assumes you have a good understanding of main parts of Docker, you have set up Docker and you have some knowledge of images and containers.

Starting and stopping containers is different from stopping and resuming ordinary processes. A process stop does not pause the process, it causes the process to exit. A stopped container is not returned by docker ps. To stop a container you use the docker stop command and pass the name of the container and the number of seconds before a container is killed. The default number of seconds the command will wait before killing is 10 seconds.

Before we demonstrate how to kill a container, let us check if there are containers running. The command below will do that.

return-containers
The docker ps command returns a list of all running containers, from our output above we do not have any running container. In the ‘learn how to use images’ tutorial, we demonstrated how to search and download images from Docker hub. We downloaded a wordpress image that we can use to create a container. Let us start a container using the wordpress image with the command below.

create-wordp
After starting the container use sudo docker ps -a command to return all containers.
return-all-containers
From the output above, we can see the container we have created. Something to note about docker is that all containers that were running at shut down will be restarted on a reboot.

We use the run command to run our container by passing the ID returned by docker ps -a to run as shown below.

Use docker ps to return only the containers that are running.
exit-status
From the output above our container shows its status as exited. When a container exits with code 0 there are no errors.

Because containers are just like any other Unix process we can interact with them via Unix signals. For example, specifying the number of seconds before a container is killed is an example of a signal. A normal process exit receives a SIGTERM signal. When a SIGTERM signal is sent but a process does not exit within the specified time, a SIGKILL signal is sent.

When stopping a container is not possible you have to kill it. To kill a container you use docker kill command and pass the container ID. You can pass any Unix signal when calling the kill command. When you kill a container you can start it again just like you start a container that was properly stopped.

When you do not need a container momentarily to perform administrative functions, it is convenient to pause other than stop containers. Pausing containers relies on cgroups freezer which blocks scheduling of frozen containers.

A key difference between pausing and stopping containers is in persistence of state. When a container is stopped any resources allocated to it such as memory are released while a paused container does not release its allocated resources.

The commands below start our firstkill container, pause it and check its status. Use the container ID returned by sudo docker ps -a.

docker-pause
The actions of creating and manipulating containers result in many image layers and container specific folders. Cleaning up these resources is important. In the next section, we look at how you can remove images and containers.

To remove a container pass the container ID to the docker rm command.

The docker images command returns a list of all images on your host. To delete an image pass the ID returned by docker images to docker rmi command. An example of deleting an image is shown below.

Our attempt to remove the helloworld image above failed because there is a running container based on the image. We should always remove a container before we can remove an image. The error gives you the ID of the container making it very simple to remove the container. Let us remove the container, then remove the image.
remove-image
In this tutorial, we discussed the difference between normal processes and docker containers. We discussed how to stop a running container. We also discussed how to kill a running container. We noted the difference between stopping and killing a container. We discussed how to pause and resume a container. Finally we discussed how to remove images and containers.

LEAVE A REPLY

Please enter your comment!
Please enter your name here