How to use docker checkpoint ls command to list container checkpoints

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker checkpoint ls command to list checkpoints for a Docker container. We will begin by creating a container and then creating a checkpoint for it.

Following the creation of a checkpoint, you will explore how to list the checkpoints associated with the container using the standard docker checkpoint ls command. Finally, you will learn how to list checkpoints when a custom checkpoint directory has been specified.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555067{{"How to use docker checkpoint ls command to list container checkpoints"}} docker/ps -.-> lab-555067{{"How to use docker checkpoint ls command to list container checkpoints"}} docker/stop -.-> lab-555067{{"How to use docker checkpoint ls command to list container checkpoints"}} docker/rm -.-> lab-555067{{"How to use docker checkpoint ls command to list container checkpoints"}} docker/pull -.-> lab-555067{{"How to use docker checkpoint ls command to list container checkpoints"}} end

Create a container and a checkpoint

In this step, we will learn how to create a Docker container and then create a checkpoint for it. Checkpointing allows you to save the state of a running container and restore it later. This is useful for debugging, migration, or creating snapshots of your application.

First, let's pull a simple image that we can use to run a container. We will use the ubuntu image.

docker pull ubuntu:latest

You should see output indicating that the image is being downloaded.

latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

Now, let's run a container from this image. We will run a simple command that keeps the container running for a while.

docker run -d --name mycontainer ubuntu:latest sleep 3600

Here's a breakdown of the command:

  • docker run: This command is used to run a new container.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • --name mycontainer: This assigns the name mycontainer to the container.
  • ubuntu:latest: This specifies the image to use for the container.
  • sleep 3600: This is the command that will be executed inside the container. It will keep the container running for 3600 seconds (1 hour).

You can verify that the container is running using the docker ps command.

docker ps

You should see output similar to this, showing your mycontainer running:

CONTAINER ID   IMAGE          COMMAND         CREATED         STATUS         PORTS     NAMES
<container_id>   ubuntu:latest   "sleep 3600"   About a minute ago   Up About a minute             mycontainer

Now that we have a running container, let's create a checkpoint for it. To create a checkpoint, you need to use the docker checkpoint create command.

docker checkpoint create mycontainer mycheckpoint1

Here's a breakdown of the command:

  • docker checkpoint create: This command is used to create a checkpoint for a container.
  • mycontainer: This is the name of the container you want to checkpoint.
  • mycheckpoint1: This is the name you are giving to the checkpoint.

If the command is successful, you will not see any output. This means the checkpoint mycheckpoint1 has been created for the container mycontainer.

List checkpoints for the container

In the previous step, we created a checkpoint named mycheckpoint1 for our container mycontainer. Now, let's learn how to list the checkpoints that exist for a specific container.

To list the checkpoints for a container, you use the docker checkpoint ls command followed by the container name.

docker checkpoint ls mycontainer

This command will display a list of all checkpoints associated with the mycontainer.

You should see output similar to this, showing the checkpoint we created in the previous step:

CHECKPOINT ID
mycheckpoint1

This output confirms that the checkpoint mycheckpoint1 exists for the container mycontainer.

You can create multiple checkpoints for the same container. Let's create another checkpoint to see how the list changes.

docker checkpoint create mycontainer mycheckpoint2

Now, list the checkpoints again:

docker checkpoint ls mycontainer

This time, the output should show both checkpoints:

CHECKPOINT ID
mycheckpoint1
mycheckpoint2

This demonstrates how to list all checkpoints associated with a particular container.

List checkpoints using a custom checkpoint directory

In the previous steps, we created checkpoints for our container mycontainer and listed them using the default checkpoint directory. Docker stores checkpoints in a default location, but you can also specify a custom directory to store your checkpoints. This can be useful for organizing checkpoints or storing them on a different volume.

To create a checkpoint in a custom directory, you use the --checkpoint-dir flag with the docker checkpoint create command. First, let's create a directory to store our custom checkpoint. We will create a directory named mycheckpoints in our home directory.

mkdir ~/project/mycheckpoints

Now, let's create a new checkpoint for mycontainer and store it in the ~/project/mycheckpoints directory.

docker checkpoint create --checkpoint-dir ~/project/mycheckpoints mycontainer mycheckpoint3

Here's a breakdown of the command:

  • docker checkpoint create: This command is used to create a checkpoint.
  • --checkpoint-dir ~/project/mycheckpoints: This flag specifies the custom directory where the checkpoint will be stored.
  • mycontainer: This is the name of the container you want to checkpoint.
  • mycheckpoint3: This is the name you are giving to this new checkpoint.

If the command is successful, you will not see any output. This means the checkpoint mycheckpoint3 has been created in the ~/project/mycheckpoints directory.

Now, to list checkpoints stored in a custom directory, you need to use the docker checkpoint ls command with the --checkpoint-dir flag, specifying the custom directory.

docker checkpoint ls --checkpoint-dir ~/project/mycheckpoints mycontainer

This command will list the checkpoints found in the specified custom directory for the container mycontainer.

You should see output similar to this, showing only the checkpoint we created in the custom directory:

CHECKPOINT ID
mycheckpoint3

Notice that this command only lists the checkpoints in the specified custom directory (mycheckpoint3) and does not show the checkpoints stored in the default location (mycheckpoint1 and mycheckpoint2).

To see all checkpoints for the container, regardless of where they are stored, you would need to list checkpoints from each directory separately or use a tool that aggregates this information. However, for listing checkpoints in a specific custom directory, the --checkpoint-dir flag is essential.

Finally, let's clean up the container we created.

docker stop mycontainer
docker rm mycontainer

This stops and removes the container mycontainer.

Summary

In this lab, we learned how to use the docker checkpoint ls command to list checkpoints for a Docker container. We started by creating a container and then creating a checkpoint for it using the docker checkpoint create command.

We then explored how to list the created checkpoints for the container using the docker checkpoint ls command. Finally, we learned how to list checkpoints located in a custom checkpoint directory, demonstrating the flexibility of managing checkpoints in different locations.