Skip to main content

Docker Container Lifecycle Explained

β€” Aman Jaiswal

Docker containers have a lifecycle that consists of several stages, and understanding this lifecycle is essential for DevOps engineers working with containerized applications.

Towards the end, we have added an image that shows the detailed lifecycle events of a contianer.

The container lifecycle refers to the different stages a container goes through, from creation to termination.

Let's break it down:

  1. Container Creation
  2. Container Running
  3. Container Paused
  4. Container Stopped
  5. Container Deleted

Let's talk about each phase one by one:

1. Container Creation

In this stage, a container is created from an image and the container is only created, not started.

We can use the docker create command to create the container:

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

2. Container Running

After creation, the container enters the running state. In this state, the application inside the container is executing. To start a container we can use docker start command:

docker start <container-name>

We can directly run the container using the docker run command:

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

Basically docker run command create and start the container in one go. We can say docker run = docker create + docker start

Here's a list of the actions and operations that can be performed on a Docker container in the running state:

  1. Exec
  2. Attach
  3. View Logs
  4. Inspect
  5. View Stats
  6. Copy Data
  7. Export/Import
  8. Commit
  9. Network Operations
  10. Monitoring and Debugging

3. Container Paused

Docker allows us to pause a container, which suspends all processes within the container. We might need to pause the containers for maintenance or debugging purposes. To pause a container we can use the docker pause command:

docker pause <container-name>

To unpause it:

docker unpause <container-name>

Here's the list of actions that can be performed on a Docker container in the paused state:

  1. Unpause
  2. Inspect
  3. Copy Data
  4. Export
  5. Commit
  6. Rename
  7. Remove
  8. Prune

4. Container Stopped

In this stage, the container is no longer running, but it still exists in Docker's storage. All the processes running in that container will be stopped. To stop a container we can use docker stop command:

docker stop <container-name>

When a Docker container is in the stopped state, the following actions can be performed:

  1. Start
  2. Restart
  3. Remove
  4. Rename
  5. Commit
  6. Export
  7. Copy Data (from the stopped container's filesystem)
  8. Inspect (view metadata and configurations)
  9. Prune (remove unused containers, images, networks, etc.)

A stop container can be started again using the docker start command.

5. Container Deleted

If we no longer need a container, we can remove it from the system using the docker rm command. This frees up the resources used by the container and cleans up Docker's storage. The container must be in the stopped state.

docker rm <container-name>
πŸ’‘
Note: We can use container-id as well instead of container-name.

After a Docker container has been deleted (removed), the following actions can be performed:

  1. Prune
  2. Inspect (view metadata and logs of the deleted container)

Detailed Lifecycle Events

Here is the image that shows the detailed lifecycle events.

Source: https://docker-saigon.github.io/post/Docker-Internals/

The Docker container lifecycle is designed to be flexible and efficient, allowing us to manage containers throughout their entire lifecycle.

We can create, run, pause, stop, restart, and remove containers as needed, making it easier to manage and scale our applications.