Saturday, November 28, 2020

Docker - Container Lifecycle

 In this post, we will explore in detail with different stages of  docker containers flow. From creating containers to  destroy containers,  there are several stages , all these stages we can call it as Docker -Containers lifecycle.

following illustration gives the entire lifecycle of docker container. 

Docker Container Lifecycle

  • When you execute docker create  with specific name and image, then container will be created with specified name, but not run the any container processes.(Created stage)
  • Then  run command will create a new container and run the image,  then container will goes to Running Stage and the container processes will run.
  • if we want to stop the running container processes, then the container stage will goes to Stopped stage and container processes will be stopped.
  • if we want to start the topped container processes, then the container stage will goes to Running stage and container processes will be run.
  • if we want to pause  the running container process, then the container stage will goes to Paused stage. in this stage container process will be suspended., but not be running.
  • if we want to unpause the suspended container process, then the container stage will goes to Running stage, and the container processes will run.
  • if we want to remove the stopped container, then the container stage will goes to deleted stage. the container will be removed.
Note:  docker kill command can be used to kill the  running container processes  immediately.

lets, have a practical stuff with these stages and get keen understandings.


1. Create container : 

docker create command will create a new Docker container with the specified image.

syntax : docker create --name <container name> <image name>

example: docker create --name cont-1 ubuntu 

Output: 


2. Run Container :

docker run command will create a new container and run the image in the newly created container. (docker run command will do the both “docker create” and “docker start” command)

syntax:  docker run -itd --name <container name> <image name>

example : docker run -itd --name cont-1 myubuntu:v1

Output : 




3. Stop Container :

docker stop command will use to stop the container which are in running stage.

syntax :  docker stop <container name>

example : docker stop cont-1

Output : 




4. Start Container :

docker start command will use to start the container which are in stopped stage.

syntax :  docker start <container name>

example : docker start cont-1

Output : 




5. Pause Container :

docker pause command will use to pause/suspend the container which are in Running stage.

syntax :  docker pause <container name>

example : docker pause cont-1

Output : 



6. Unpause Container :

docker unpause command will use to run the container which are in paused stage.

syntax :  docker unpause <container name>

example : docker unpause cont-1

Output : 




7. Remove Container :

docker rm command will use to delete the container which are in stopped or Created  stages.

syntax :  docker rm <container name>

example : docker rm cont-1

Output : 




docker rm -f command will use to delete the container which is in Running stage.

syntax :  docker rm -f  <container name>

example : docker rm -f  cont-1

Output :