How to use docker container cp command to copy files

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker container cp command to copy files between your local filesystem and Docker containers. This hands-on experience will cover essential scenarios, including copying files from your local machine into a running container, retrieving files from a container to your local system, and utilizing streaming capabilities to transfer data via STDOUT and STDIN.

Through practical examples, you will gain proficiency in managing data flow for your containerized applications. You will learn how to copy individual files, directories, and even stream data, providing you with the flexibility needed for various development and deployment tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/start("Start Container") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/VolumeOperationsGroup -.-> docker/cp("Copy Data Between Host and Container") subgraph Lab Skills docker/run -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/ps -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/start -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/stop -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/rm -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/exec -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/pull -.-> lab-555105{{"How to use docker container cp command to copy files"}} docker/cp -.-> lab-555105{{"How to use docker container cp command to copy files"}} end

Copy a local file into a container

In this step, you will learn how to copy a file from your local filesystem into a running Docker container. This is a common task when you need to add configuration files, scripts, or other data to a container after it has been started.

First, let's create a simple text file on your local machine within the ~/project directory.

echo "This is a test file." > ~/project/test_file.txt

This command creates a file named test_file.txt in your ~/project directory and writes the text "This is a test file." into it.

Next, we need a running container to copy the file into. We will use a simple ubuntu image for this purpose. Since the image might not be present locally, we will first pull it.

docker pull ubuntu:latest

This command downloads the latest version of the ubuntu image from Docker Hub.

Now, let's run a container based on the ubuntu image. We will run it in detached mode (-d) and give it a name (my_ubuntu_container) for easy identification.

docker run -d --name my_ubuntu_container ubuntu:latest sleep infinity

This command starts a new container named my_ubuntu_container using the ubuntu:latest image. The sleep infinity command keeps the container running indefinitely, which is useful for demonstration purposes.

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

docker ps

You should see my_ubuntu_container listed in the output.

Now, we can copy the test_file.txt from your local machine into the running container using the docker cp command. The syntax is docker cp <local_path> <container_name>:<container_path>.

docker cp ~/project/test_file.txt my_ubuntu_container:/tmp/

This command copies the file ~/project/test_file.txt into the /tmp/ directory inside the my_ubuntu_container.

To verify that the file was successfully copied, you can execute a command inside the container using docker exec. We will use ls to list the files in the /tmp/ directory of the container.

docker exec my_ubuntu_container ls /tmp/

You should see test_file.txt listed in the output, confirming that the file was copied successfully.

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

docker stop my_ubuntu_container
docker rm my_ubuntu_container

These commands stop and remove the container, respectively.

Copy files from a container to the local filesystem

In this step, you will learn how to copy files from a running Docker container to your local filesystem. This is useful for retrieving logs, configuration files, or any other data generated or modified within a container.

First, let's ensure we have a running container to work with. We will use the my_ubuntu_container from the previous step. If it's not running, start it.

docker start my_ubuntu_container

This command starts the container named my_ubuntu_container.

Now, let's create a file inside the container that we will copy to our local machine. We will use docker exec to run a command inside the container to create a file named container_file.txt in the /root/ directory.

docker exec my_ubuntu_container sh -c 'echo "This file is from the container." > /root/container_file.txt'

This command executes a shell command inside my_ubuntu_container to create the file /root/container_file.txt with the specified content.

To verify that the file was created inside the container, you can use docker exec to list the files in the /root/ directory of the container.

docker exec my_ubuntu_container ls /root/

You should see container_file.txt listed in the output.

Now, we can copy the container_file.txt from the container to your local machine using the docker cp command. The syntax for copying from a container to local is docker cp <container_name>:<container_path> <local_path>. We will copy it to your ~/project directory.

docker cp my_ubuntu_container:/root/container_file.txt ~/project/

This command copies the file /root/container_file.txt from my_ubuntu_container to your local ~/project/ directory.

To verify that the file was successfully copied to your local machine, list the files in your ~/project directory.

ls ~/project/

You should see container_file.txt listed in the output, along with test_file.txt from the previous step.

You can also view the content of the copied file using the cat command.

cat ~/project/container_file.txt

The output should be "This file is from the container.", confirming the content was copied correctly.

Finally, let's clean up the container.

docker stop my_ubuntu_container
docker rm my_ubuntu_container

Stream a file from a container to STDOUT

In this step, you will learn how to stream the content of a file from a running Docker container directly to your local machine's standard output (STDOUT). This is useful for quickly viewing the content of a file within a container without explicitly copying it.

First, ensure the my_ubuntu_container is running. If not, start it.

docker start my_ubuntu_container

Now, let's create another file inside the container that we will stream to STDOUT. We will create a file named stream_test.log in the /var/log/ directory of the container.

docker exec my_ubuntu_container sh -c 'echo "This is a log entry." > /var/log/stream_test.log'

This command executes a shell command inside my_ubuntu_container to create the file /var/log/stream_test.log with the specified content.

To stream the content of this file to your local STDOUT, you can use the docker exec command with the cat command inside the container.

docker exec my_ubuntu_container cat /var/log/stream_test.log

This command executes the cat /var/log/stream_test.log command inside the my_ubuntu_container. The output of this command within the container is then streamed to your local terminal's STDOUT.

You should see the content "This is a log entry." printed directly to your terminal. This demonstrates how you can quickly view the content of a file inside a container without needing to copy the entire file.

Finally, let's clean up the container.

docker stop my_ubuntu_container
docker rm my_ubuntu_container

Stream a local tar archive to a container from STDIN

In this step, you will learn how to stream a local tar archive directly into a running Docker container using standard input (STDIN). This is an efficient way to transfer multiple files or directories into a container without creating intermediate files on your local machine.

First, let's create a directory and some files on your local machine within the ~/project directory that we will archive and stream.

mkdir ~/project/my_files
echo "File 1 content." > ~/project/my_files/file1.txt
echo "File 2 content." > ~/project/my_files/file2.txt

This creates a directory my_files and two text files inside it.

Now, ensure the my_ubuntu_container is running. If not, start it.

docker start my_ubuntu_container

To stream a tar archive to the container, we will use the tar command to create an archive of the my_files directory and pipe (|) its output to the docker exec command. Inside the container, we will use the tar command again to extract the archive.

We will stream the archive to the /opt/ directory inside the container.

tar -cvf - ~/project/my_files | docker exec -i my_ubuntu_container tar -xvf - -C /opt/

Let's break down this command:

  • tar -cvf - ~/project/my_files: This command creates a tar archive (-c) verbosely (-v) of the ~/project/my_files directory. The -f - option tells tar to write the archive to standard output.
  • |: This pipes the standard output of the tar command (the tar archive) to the standard input of the next command.
  • docker exec -i my_ubuntu_container tar -xvf - -C /opt/: This executes the tar command inside the my_ubuntu_container. The -i flag is crucial as it keeps STDIN open for the exec command, allowing it to receive the piped data. The tar -xvf - command extracts (-x) verbosely (-v) from standard input (-f -). The -C /opt/ option changes the directory to /opt/ before extracting, so the contents of the archive will be extracted into /opt/my_files inside the container.

To verify that the files were successfully streamed and extracted inside the container, list the contents of the /opt/my_files/ directory within the container using docker exec.

docker exec my_ubuntu_container ls /opt/my_files/

You should see file1.txt and file2.txt listed in the output, confirming that the tar archive was streamed and extracted correctly.

Finally, let's clean up the container and the local files.

docker stop my_ubuntu_container
docker rm my_ubuntu_container
rm -rf ~/project/my_files

Summary

In this lab, you learned how to use the docker container cp command to copy files between your local filesystem and a running Docker container. You practiced copying a local file into a container, demonstrating how to add necessary data or configurations after a container is started. This involved creating a local file, pulling and running a simple Ubuntu container, and then using docker cp to transfer the file into the container's /tmp directory.