Docker is a containerization platform for developing, shipping, and running applications inside containers. We can deploy many containers simultaneously on a given host. Containers are very fast and boot up quickly because they don’t need the extra load of a hypervisor in comparison to the virtual machines because they run directly within the host machine’s kernel. In this blog, we will talk about Docker Container Lifecycle Management.
Docker Container Lifecycle Management
There are different stages when we create a Docker container which is known as Docker Container Lifecycle. Some of the states are:
- Created: A container that has been created but not started
- Running: A container running with all its processes
- Paused: A container whose processes have been paused
- Stopped: A container whose processes have been stopped
- Deleted: A container in a dead state

Commands in Docker Container Lifecycle Management
Managing the states of the Docker containers is called Docker Container Lifecycle Management. We have to assure that the containers are up and running, or destroyed if they are of no use anymore. For managing the Docker Lifecycle we have some common commands which are explained below.
Create Containers
Using the docker create command will create a new Docker container with the specified docker image.
$ docker create --name <container name> <image name>

Start Container
To start a stopped container, we can use the docker start command.
$ docker start <container name>

Run Container
The docker run command will do the work of both “docker create” and “docker start” command. This command will create a new container and run the image in the newly created container.
$ docker run -it --name <container name> <image name>

Check Out: Docker Tutorial for Beginners. Click here
Pause Container
If we want to pause the processes running inside the container, we can use the “docker pause” command.
$ docker pause <container name>

To unpause the container, use “docker unpause” command.
$ docker unpause <container name>

Stop Container
Stopping a running Container means to stop all the processes running in that Container. Stopping does not mean killing or ending the process.
$ docker stop <container name>

A stopped container can be made into the start state, which means all the processes inside the container will again start. When we do the docker stop command, the main process inside the container receives a SIGTERM signal. If you got confused about the term SIGTERM, then need not worry we will cover these signals later in the blog.
We can stop all the containers using a single command. In our case, 4 containers are running which you can see using the docker ps command.

To stop all the running containers we can use the following command:
$ docker stop $(docker container ls –aq)

Check Out: How to Fix Vulnerabilities in Docker Images. Click here
Delete Container
Removing or deleting the container means destroying all the processes running inside the container and then deleting the Container. It’s preferred to destroy the container, only if present in the stopped state instead of forcefully destroying the running container.

As we tried deleting a Container which was in running state, so the docker daemon throws an error. We have to first stop the container and delete it.
$ docker stop <container name>
$ docker rm <container name>

We can delete or remove all containers with a single command only. In our example, 4 containers (not necessarily running) are there which you can see using the docker ps -a command.
We can see there are 4 containers which are not in the running state. Now we will delete all of them using a single command which is given below:
$ docker rm $(docker ps -aq)

Kill Container
We can kill one or more running containers.
$ docker kill <container name>

Difference between docker run, docker Create And Docker Start
Docker create command creates a new container from the specified image. However, it will not run the container immediately.
Docker start command is used to start any stopped container. If we used the docker to create a command to create a container, then we can start it with this command.
Docker run command is a combination of creating and start as it creates a new container and starts it immediately. In fact, the docker run command can even pull an image from Docker Hub if it doesn’t find the mentioned image on your system.
Also read: Container (Docker) vs Virtual Machines (VM) to understand what is their difference.
Difference Between Docker Pause And Docker Stop container
The docker pause command suspends all processes in the specified containers. Traditionally, when suspending a process the SIGSTOP signal is used, which is observable by the process being suspended. Also, the memory portion would be there while the container is paused and again the memory is used when the container is resumed.
When we use the docker stop command, the main process inside the container receives SIGTERM signal, and after some time, SIGKILL. Also, it will release the memory used after the container is stopped.
SIGTERM is the signal of termination. The intention is to kill the process, gracefully or not, but to first allow it a chance to clean up.
SIGKILL is the kill signal. The only behaviour is to kill the process, immediately.
SIGSTOP is the pause signal. The only behaviour is to pause the process. The shell uses pausing (and its counterpart, resuming via SIGCONT) to implement job control.
Check out this article on Docker Compose
Docker rm Vs. Docker Kill
docker container rm: Using docker rm, we can remove one or more containers from the host node and for doing container name or ID can be used.
docker container kill: The main process inside each container specified will be sent SIGKILL or any signal specified with option –signal.
Read this blog to know about what is Kubernetes Pod which is an important component of Kubernetes.