Docker Container Lifecycle


Docker container
Docker

To understand the concept of Docker containers or containerization in general, it’s important to first understand the Docker container lifecycle. Maintaining a microservice application deployed through containers with complex requirements is not easy. To add to it, the only possible way to maintain Docker containers is through a command line. Hence, keeping track of each Docker container through a single command line becomes difficult.

Docker comes packed with tools and commands to manage our containers in the most efficient manner. Hence, leveraging these commands will make your lives a lot easier. If you know your Docker container commands, spinning up a Docker container is just a piece of cake.

In this tutorial of the Docker tutorial series, we will discuss the Docker container lifecycle in detail. We will discuss all the possible states in the Docker lifecycle and see how to manage containers in all these states with the help of corresponding Docker commands.

Before we begin with this tutorial, check out the detailed explanation of What is Docker? to understand the Docker Container lifecycle better.

States in Docker Container Lifecycle

Docker container lifecycle
Docker Container Lifecycle

In the above diagram, you can find all the possible states in a Docker Container lifecycle. However, to simplify things, let’s refer to another diagram which is a bit easy to understand.

Docker container lifecycle
Docker Container Lifecycle

Let’s discuss each of the steps and states in a Docker Container lifecyle stepwise below.

  • A Docker user first pulls an image from a Docker registry such as Dockerhub. This image can be used as it is or as a base image to build custom Docker images using Dockerfiles. The command to pull a Docker image from Dockerhub is –
$  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Docker Pull Command
Docker Pull Command
  • Next, you can execute the Docker create command on the image that you have pulled in the previous step. This will create a Docker container associated with that image. The container is now in the created state. The syntax of the Docker create command is –
$  docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Docker Create Command
Docker Create Command
Listing Docker Containers
Listing Docker Containers
  • After you have created the Docker container, you can invoke the start command on the container to run the container. Once you have invoked the Docker start command, you will have access to the container’s filesystem and the environment. The container is now in started state. Note that the container has just started and is not actively running.
$ docker start [OPTIONS] CONTAINER [CONTAINER...]
Docker Start Command
Docker Start Command
  • You can also directly invoke the Docker run command after pulling the image from Dockerhub or even without pulling the image. When we execute the Docker run command, the daemon first checks for an existing image with the same digest locally. If it finds one, this image is used to run the container. If not, the image is automatically pulled from Dockerhub. Once we invoke the Docker run command, the container comes to the running state.
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Docker Run Command
Docker Run Command
  • Now, you can either invoke Docker pause command to transfer the container in the paused state or you can use the Docker stop command to stop the container and transfer it to exited state. By pausing or stopping containers, we speak in terms of stopping/pausing the processes inside the container. We can use the Docker start command to start a stopped container or the unpause command to unpause the container.
$ docker start [OPTIONS] CONTAINER [CONTAINER...]
$ docker stop [OPTIONS] CONTAINER [CONTAINER...]
Docker Stop Command
Docker Stop Command

Please note that the docker ps command is used to list all the containers that are actively running. The docker ps -a command is used to list all the existing containers.

$ docker pause CONTAINER [CONTAINER...]
$ docker unpause CONTAINER [CONTAINER...]
Docker Pause and Unpause Commands
Docker Pause and Unpause Commands
  • If the daemon tries to get hold of the container but is not able to, mostly due to the container being busy, then it goes to dead state. We can use the Docker kill or rm commands to kill or delete a container.
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]
$ docker kill [OPTIONS] CONTAINER [CONTAINER...]

Let’s try to remove multiple containers using the docker rm command.

Docker rm Command
Docker rm Command

Difference between Docker Create, Start, and Run Commands

The Docker Create command is used to create a new instance of the specified image. When we execute the Docker create command on an image, a container associated to that image is simply created. However, the container does not start running.

The Docker Start command is used to start those containers that were previously stopped using the Docker stop command.

The Docker Run command can be understood as a combination of Docker pull, create, and start commands. If the image specified with the Docker run command does not exist, then the Docker daemon automatically pulls the image. If it exists, then it invokes the start command to create a container and run the container automatically. All these happens in the background.

Difference between Docker Pause and Stop Commands

When we invoke the Docker Pause command on a container, the daemon pauses all the processes running inside the container using the SIGSTOP signal. The memory of the container still persists and is once again available when the container processes resumes using the Docker unpause command.

When we invoke the Docker Stop command on a container, the daemon invokes the SIGTERM signal to the main process of the container and then invokes the SIGKILL signal. The entire memory of the container is released.

The SIGKILL signal is used to directly kill the process immediately.

The SIGTERM signal also kills the process, but it kills it gracefully. In other words, it provides a chance to the process to first get cleaned up.

The SIGSTOP signal is used to pause the process. It’s counterpart is SIGCONT signal.

Wrapping Up!

To sum up, in this tutorial, we have discussed the Docker Container lifecycle in a great detail. We have also discussed all the states in the Docker Container lifecycle along with the commands used to bring the containers to these states through practical hands-on example.

If you have any queries, suggestions, or doubts, please mention them in the comments and we will have our expert get back to you as soon as possible. If you liked this article, please give a thumbs up in the comments. Our users’ happiness is what drives us to create quality content like this article on Docker Container Lifecycle.

Also, check out the complete series of tutorials on Docker here. If you are not aware of what Docker is, our article on What is Docker? is the best place for you to get started.

2 comments

Leave a Reply