How to use docker checkpoint create command to checkpoint a running container

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker checkpoint create command to save the state of a running container. We will begin by starting a simple Ubuntu container that will serve as the target for our checkpointing operations.

Following the container setup, you will create a checkpoint for the running container, effectively capturing its current state. We will then verify that the checkpoint has been successfully created. Finally, you will explore how to create a checkpoint and specify a custom directory for storing the checkpoint data. This hands-on experience will demonstrate the practical application of Docker container checkpointing.


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/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") subgraph Lab Skills docker/run -.-> lab-555066{{"How to use docker checkpoint create command to checkpoint a running container"}} docker/ps -.-> lab-555066{{"How to use docker checkpoint create command to checkpoint a running container"}} docker/create -.-> lab-555066{{"How to use docker checkpoint create command to checkpoint a running container"}} docker/pull -.-> lab-555066{{"How to use docker checkpoint create command to checkpoint a running container"}} docker/images -.-> lab-555066{{"How to use docker checkpoint create command to checkpoint a running container"}} end

Start a container to be checkpointed

In this step, we will start a simple Docker container that we will later checkpoint. Checkpointing allows you to save the state of a running container and restore it later. This is useful for migrating containers, debugging, or creating snapshots.

First, we need to pull the ubuntu image from Docker Hub. This image is small and suitable for our demonstration. Open your terminal in the LabEx environment.

docker pull ubuntu

You should see output indicating that the image is being downloaded. Once the download is complete, you can verify that the image is available locally by listing the images.

docker images

Look for ubuntu in the list of images.

Now, let's start a container based on the ubuntu image. We will run a simple command inside the container that keeps it running, like sleep infinity. We will also give the container a name so it's easier to reference later. Let's name it my-checkpoint-container.

docker run -d --name my-checkpoint-container ubuntu sleep infinity

The -d flag runs the container in detached mode, meaning it runs in the background. The --name flag assigns a name to the container. ubuntu is the image we are using, and sleep infinity is the command that will run inside the container to keep it alive.

After running the command, Docker will output the full container ID. You can verify that the container is running by listing the active containers.

docker ps

You should see a container named my-checkpoint-container in the list with a status of Up. This confirms that our container is running and ready to be checkpointed in the next steps.

Create a checkpoint for the running container

In this step, we will create a checkpoint for the running container we started in the previous step. Checkpointing a container saves its current state, including its memory, processes, and file system changes, to disk. This allows you to pause a container and resume it later from the exact point it was checkpointed.

To create a checkpoint, we use the docker checkpoint create command. This command requires the name of the container and a name for the checkpoint. Let's name our checkpoint my-first-checkpoint.

docker checkpoint create my-checkpoint-container my-first-checkpoint

This command will take a moment to execute as Docker saves the container's state. You won't see much output if the command is successful.

After creating the checkpoint, the container my-checkpoint-container will still be running. Checkpointing does not stop the container. It simply creates a snapshot of its state.

In the next step, we will verify that the checkpoint was successfully created and is available.

Verify the created checkpoint

In this step, we will verify that the checkpoint we created in the previous step exists and is associated with our container. Docker provides a command to list the checkpoints for a specific container.

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

docker checkpoint ls my-checkpoint-container

This command will display a list of checkpoints available for the specified container. You should see my-first-checkpoint listed in the output. This confirms that the checkpoint was successfully created and is ready to be used for restoring the container's state if needed.

Being able to list checkpoints is important for managing them, especially when you have multiple checkpoints for a single container or checkpoints for different containers.

In the next step, we will explore creating a checkpoint in a different directory.

Create a checkpoint with a custom directory

In this step, we will learn how to create a checkpoint in a specific directory of our choice. By default, Docker stores checkpoints in a standard location within the Docker data root directory. However, you might want to store checkpoints in a different location, for example, on a different disk or a shared storage.

To specify a custom directory for the checkpoint, we use the --checkpoint-dir flag with the docker checkpoint create command. First, let's create a new directory in our home directory where we will store the checkpoint.

mkdir ~/project/my-checkpoints

Now, we can create another checkpoint for our running container my-checkpoint-container, but this time, we will specify the ~/project/my-checkpoints directory as the checkpoint location. Let's name this new checkpoint my-second-checkpoint.

docker checkpoint create --checkpoint-dir ~/project/my-checkpoints my-checkpoint-container my-second-checkpoint

This command will create the checkpoint data within the ~/project/my-checkpoints directory. You can verify the contents of this directory to see the checkpoint files.

ls ~/project/my-checkpoints/my-second-checkpoint

You should see several files and directories created by the checkpointing process.

Even though we specified a custom directory, the checkpoint is still associated with the container. You can list the checkpoints for the container again, and you will see both my-first-checkpoint and my-second-checkpoint listed.

docker checkpoint ls my-checkpoint-container

This shows that Docker keeps track of checkpoints regardless of their storage location. Using a custom directory gives you more flexibility in managing your checkpoint data.

Summary

In this lab, we learned how to use the docker checkpoint create command to save the state of a running container. We began by starting a simple ubuntu container named my-checkpoint-container using docker run -d --name my-checkpoint-container ubuntu sleep infinity, ensuring it was running in detached mode. We verified the container's status using docker ps.

Following this, we proceeded to create a checkpoint for the running container. This process involves using the docker checkpoint create command to capture the container's current state, including its memory and processes, allowing for later restoration. We also explored how to verify the successful creation of the checkpoint and how to specify a custom directory for storing the checkpoint data.