How to use docker container export command to export a container's filesystem

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker container export command to export the filesystem of a Docker container. We will begin by creating and running a simple container.

Following the container creation, you will explore different methods for exporting the container's filesystem. This includes exporting the filesystem directly to standard output (STDOUT), exporting it to a file using shell redirection, and finally, exporting it to a file using the --output option of the docker container export command.


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/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ImageOperationsGroup -.-> docker/save("Save Image") subgraph Lab Skills docker/run -.-> lab-555109{{"How to use docker container export command to export a container's filesystem"}} docker/ls -.-> lab-555109{{"How to use docker container export command to export a container's filesystem"}} docker/ps -.-> lab-555109{{"How to use docker container export command to export a container's filesystem"}} docker/save -.-> lab-555109{{"How to use docker container export command to export a container's filesystem"}} end

Create and run a simple container

In this step, we will learn how to create and run a simple Docker container. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

We will use the docker run command to create and run a container. The basic syntax is docker run [OPTIONS] IMAGE [COMMAND] [ARG...].

Let's run a simple container using the ubuntu image and the echo command. The ubuntu image is a minimal Ubuntu operating system image.

docker run ubuntu echo "Hello from Docker!"

When you run this command for the first time, Docker will first check if the ubuntu image exists locally. If it doesn't, it will pull the image from Docker Hub (the default registry). Then, it will create a new container from this image and execute the echo "Hello from Docker!" command inside the container. After the command finishes, the container will stop.

You should see the output Hello from Docker! in your terminal.

Now, let's try running a container that stays running. We can use the -d option to run the container in detached mode (in the background) and the tail -f /dev/null command to keep the container running without consuming significant resources. We will also give the container a name using the --name option so we can easily refer to it later.

docker run -d --name my-ubuntu-container ubuntu tail -f /dev/null

This command will pull the ubuntu image (if not already present), create a container named my-ubuntu-container, and run the tail -f /dev/null command in detached mode. The command will output the container ID.

To verify that the container is running, you can use the docker ps command, which lists running containers.

docker ps

You should see a list of running containers, including my-ubuntu-container.

Export the container's filesystem to STDOUT

In this step, we will learn how to export the filesystem of a Docker container. Exporting a container's filesystem creates a tar archive of the container's contents. This can be useful for creating backups, sharing container states, or inspecting the contents of a container.

We will use the docker export command to export the filesystem. The basic syntax is docker export [OPTIONS] CONTAINER.

Let's export the filesystem of the my-ubuntu-container we created in the previous step. By default, docker export outputs the tar archive to standard output (STDOUT).

docker export my-ubuntu-container

When you run this command, you will see a stream of binary data in your terminal. This is the tar archive of the container's filesystem. Since it's binary data, it won't be human-readable in the terminal.

To get a better understanding of what's being exported, we can pipe the output to a command that can process tar archives, like tar. However, for this step, we are just focusing on exporting to STDOUT.

The docker export command exports the entire filesystem of the container, including any changes you've made since the container was created.

Export the container's filesystem to a file using redirection

In the previous step, we exported the container's filesystem to STDOUT. While this is useful for piping the output to other commands, it's often more practical to save the exported filesystem to a file.

We can achieve this by using shell redirection. The > operator redirects the standard output of a command to a file.

Let's export the filesystem of the my-ubuntu-container to a file named ubuntu-filesystem.tar in your current directory (~/project).

docker export my-ubuntu-container > ubuntu-filesystem.tar

This command will execute docker export my-ubuntu-container and redirect the binary output (the tar archive) to the file ubuntu-filesystem.tar. You won't see the binary data in your terminal this time.

After the command finishes, you can verify that the file was created and check its size using the ls -lh command.

ls -lh ubuntu-filesystem.tar

You should see the file ubuntu-filesystem.tar listed with its size. The size will depend on the contents of the ubuntu image.

You can also use the file command to confirm that the created file is a tar archive.

file ubuntu-filesystem.tar

The output should indicate that the file is a tar archive, for example, ubuntu-filesystem.tar: POSIX tar archive.

Export the container's filesystem to a file using the --output option

In the previous step, we used shell redirection (>) to save the exported container filesystem to a file. Docker also provides a dedicated option, --output or -o, to specify the output file directly with the docker export command. This can sometimes be more convenient than using shell redirection.

Let's export the filesystem of the my-ubuntu-container again, this time using the --output option, and save it to a file named ubuntu-filesystem-output.tar in your current directory (~/project).

docker export --output ubuntu-filesystem-output.tar my-ubuntu-container

This command is equivalent to using redirection, but it uses a Docker-specific option. The tar archive will be saved directly to the specified file.

After the command completes, you can verify the creation and size of the new file using ls -lh.

ls -lh ubuntu-filesystem-output.tar

You should see the file ubuntu-filesystem-output.tar listed. Its size should be similar to the ubuntu-filesystem.tar file created in the previous step.

You can also use the file command to confirm that ubuntu-filesystem-output.tar is a tar archive.

file ubuntu-filesystem-output.tar

The output should confirm that it is a tar archive.

Using the --output option is a clean way to specify the destination file for the exported filesystem.

Summary

In this lab, we learned how to create and run simple Docker containers. We used the docker run command to execute commands within a container and to run containers in detached mode with a specific name. We also used the docker ps command to verify that a container is running.

We then explored how to export a container's filesystem using the docker export command. We learned how to export the filesystem to standard output (STDOUT) and how to redirect the output to a file. Finally, we learned how to use the --output option with docker export to specify the output file directly.