Everything has a lifecycle that goes through a different state from start to finished. Similarly, when a container is created, it has its own lifecycle. In this short article, we’ll explain the lifecycle of the Docker container and what are the different commands that are used to jump from one state to another.

The below picture depicts the lifecycle of the Docker container:

As the image is self-explanatory, but let me give an explanation of each and every state of the container’s lifecycle along with the commands that are used to manage the lifecycle of a container.

created

A docker’s container will be in the created stated when a user creates a container using the below command:

[root@instance-20191018-2102 ~]# docker container create --name nginx01 nginx

The output of the above command will be as per the following screenshot:

running

Once a container is created, you can start the container and once a container is started the container status will change to running. The following command is used to start the container:

[root@instance-20191018-2102 ~]# docker container start nginx01

The output of the above command will be as per the following screenshot:

Alternatively, you can use the run command to combine create and start commands. The run command will create and start the container. The following is the command to run the container:

[root@instance-20191018-2102 ~]# docker container run -d  --name nginx02 nginx

-d: Option is used to run the container in detached mode.

paused

You can pause the processes for temporary purposes. To do so run the following command:

[root@instance-20191018-2102 ~]# docker container pause nginx01

The output of the above command will be as per the following screenshot:

To unpause the container, run the following command:

[root@instance-20191018-2102 ~]# docker container unpause nginx01

The output of the above command will be as per the following screenshot:

stoped

To stop the container, run the following command:

[root@instance-20191018-2102 ~]# docker container stop  nginx01

deleted

The container should be in the stoped state to delete it. So, first, run the stop command to stop the container and then run the below command to delete the container:

[root@instance-20191018-2102 ~]# docker container rm nginx01

Once the container is deleted, it can’t be recovered.

This is how the container’s lifecycle works and is managed using the docker commands.

Further readings

How to create a docker container

Architecture and basic terminologies of Docker

How to create an NGINX docker container

Look to the video on how to create a docker container:

You can subscribe to my YouTube channel for further reading.