Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1891

Docker Container Lifecycle Management: create, run, pause, stop and delete

$
0
0

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 the Docker Container Lifecycle Management.

Know more about the difference between Container and Virtual Machines

In this blog, we will be covering the following topics:

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

Docker Container lifecycleCommands in Docker Container Lifecycle Management

Managing the states of Docker container 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>

docker create commandStart Container

To start a stopped container, we can use the docker start command.

$ docker start <container name>

Container lifecycle management

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>

docker Container lifecycle management

Pause Container

If we want to pause the processes running inside the container, we can use the “docker pause” command.

$ docker pause <container name>

docker pause
To unpause the container, use “docker unpause” command.

$ docker unpause <container name>

docker unpause

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>

docker stop command
A stopped container can be made into the start state, which means to all the processes inside the container will again start. When we do 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 docker ps command.

docker management

To stop all the running containers we can use the following command:

$ docker stop $(docker container ls –aq)

stopping all the containersDelete 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.

docker rm
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>

docker lifecycle

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.

To list all the containersWe 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)

delete all containers

Kill Container

We can kill one or more running containers.

$ docker kill <container name>

docker container managementDifference 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 docker create command to create a container, then we can start it with this command.

Docker run command is a combination of create 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.

Difference between docker pause and docker stop

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 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.

Difference between docker rm and 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.

Related Post/References

Join FREE Masterclass

To know about what is Docker, about the Docker Architecture, how to install Docker on our system, how docker commands work, what is Docker images and how to create one from the scratch, about the Docker Networking and Storage. We also provide Hands-On labs that you must perform to clear Certified Kubernetes Administrator (CKA) certification exam by registering for our FREE Masterclass.

Free Masterclass

The post Docker Container Lifecycle Management: create, run, pause, stop and delete appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1891

Trending Articles