How to Copy Files with Docker cp to your Docker Container

Published:6 September 2021 - 7 min. read

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

If you need to copy files from the Docker host to your Docker container, this tutorial is for you.

In this tutorial, you will learn to copy files from the Docker host to a Docker container using various approaches.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

Let’s get to it!

Prerequisites

If you’d like to follow along step-by-step, ensure you have the following:

  • Docker installed on the Linux host. This tutorial uses Docker v19.03.11. You can confirm your Docker version by running docker version.

Copying Files with the docker cp Command

To start this tutorial, you will learn to copy files from Docker host to the containers using the docker cp command. The docker cp command copies files or folders between a container and the local filesystem of your Docker host and vice versa.

Let’s learn how to use Docker cp command with an example.

1. Open a terminal on your local machine.

2. Create a file named myfile.txt using the touch command. The myfile.txt will be copied from the docker host to the container.

touch myfile.txt

3. Execute the docker run command. The below docker run will create a new container in the background. The below command contains three parameters described below:

  • d flag that runs the container in the background and keeps it alive until deleted.
  • p flag publishes a container’s port 80 to the host on port 80.
  • nginx will be the image that will be used to run the container.
sudo docker run -d -p 80:80 nginx
Executing the Docker run command
Executing the Docker run command

4. Verify if the Docker container is successfully created using the docker ps command. After you execute the docker ps command, you should see a new value generated under the attribute CONTAINER ID (In the below case ccae4670f030) using the NGINX image that confirms the container is successfully created.

sudo docker ps
Verifying the docker container by running the docker ps command
Verifying the docker container by running the docker ps command

Before you execute the Docker cp command, The syntax of the Docker cp command is:

  • CONTAINER: SRC_PATH specifies the source path of the container.
  • DEST_PATH is the destination path on the host.
  • CONTAINER: DEST_PATH is the destination path on the container.

Additionally, you can also add the options with the below parameters in the command as follows:

  • Using archive or a – Copies all the user and primary group permissions of the files and folders.
  • Using L – Specifying the L option will allow any symbolic link in the Source Path to be copied to the destination path.
# Syntax to Copy from Container to Docker Host  
docker cp {options} CONTAINER:SRC_PATH DEST_PATH 
# Syntax to Copy from Docker Host to Container  
docker cp {options} SRC_PATH CONTAINER:DEST_PATH 

5. Next, execute the docker cp command. The docker cp command will copy the myfile.txt that you created earlier to the containers /usr/share directory. ccae4670f030 is the container ID to which the myfile.txt will be copied.

sudo docker cp myfile.txt ccae4670f030:/usr/share

6. Finally, SSH into the running container by running the docker exec command with /bin/bash, which is used as the default shell for user login of the Linux system.

  • The i flag indicating you’d like to open an interactive SSH session to the container. The i flag does not close the SSH session even if the container is not attached.
  • The t flag allocates a pseudo-TTY which much be used to run commands interactively.sudo docker exec -it ccae4670f030 /bin/bash
sudo docker exec -it ccae4670f030 /bin/bash

You’ll see below that you’re now connected to the container’s shell when you run docker exec.

SSH into the running container using docker exec command
SSH into the running container using docker exec command

7. After logging in to the container, verify if myfile.txt has been copied to the container using the ls command.

  • ls is a command to list computer files in Unix and Unix-like operating systems.
  • grep will search for all files or folders, starting with string my inside the usr/share directory.
Verifying the files in the container
Verifying the files in the container

Copying Files using DockerFile

In the previous section, you learned how to copy the files into the container by running the Docker cp command. What if you need to copy multiple files in one go? Certainly, running multiple commands becomes an overhead! To solve running multiple cp commands, why not try copying files or folders into containers using Dockerfile with COPY commands?

Deploying a container and copying the files/folders using Dockerfile allows you to remove the manual copying steps you did in the previous section. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Let’s create a Dockerfile, run a container from it, and finally copy the files.

1. Create a folder named ~/host-to-container-copy-demo, then change (cd) the working directory to that folder. This folder will contain all of the files you’ll be creating in this demo.

mkdir ~/host-to-container-copy-demo
cd ~/host-to-container-copy-demo

2. Now, create two text files named myfile1.txt and myfile2.txt, copy and paste them into the files, and save them to the ~/host-to-container-copy-demo directory.

# myfile1.txt
Hello This is my first file !
This is file will be copied in /usr/share directory from Docker host to Docker Container.
# myfile2.txt
Hello This is my second file !
This is file will be copied in /tmp directory from Docker host to Docker Container.

3. Create another file, copy/paste the below configuration, and save the file as Dockerfile inside the ~/host-to-container-copy-demo directory. When complete, Docker will use this DockerFile to run all the commands necessary to build a new Docker image on top of any base image.

The DockerFile below contains various steps/instructions that will build the new container:

  • FROM The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
  • COPY COPY command copies a file from the host machine (Docker host) to the container.
# Instruction for Dockerfile to create a new image on top of the base image (ubuntu)
# Using the base image ubuntu: latest
FROM ubuntu:latest
# Copying myfile1.txt to the containers /usr/share directory
COPY myfile1.txt /usr/share
# Copying myfile2.txt to the containers /tmp directory
COPY myfile2.txt /tmp

4. Verify all of the required files to build the new image by running the tree command. You should see Dockerfile, myfile1.txt, and myfile2.txt under the ~/host-to-container-copy-demo directory.

Verifies files and folders using tree command
Verifies files and folders using tree command

5. Next, build the image by running the docker build command. The t flag is used to tag the image updated_ubuntu_image with the latest and. allows docker to pick all the necessary files from the present working directory.

sudo docker build -t updated_ubuntu_image:latest .
Building the Docker image by running the docker build command
Building the Docker image by running the docker build command

6. Now, verify the newly built image updated_ubuntu_image by running the docker images command. Note the REPOSITORY attribute. This attribute is the tag created with the -t flag in the previous step.

sudo docker images
Repository Attribute
Repository Attribute

7. Finally, run the docker container using the newly built image by running the docker run command. The -it flag instructs Docker to allocate a pseudo-terminal connected to the container’s stdin. bash provides the default shell for user login of the Linux system.

sudo docker run -it updated_ubuntu_image bash

You’ll see below that you’re now in the Docker container’s Bash shell.

Running the container using docker run command.
Running the container using docker run command.

8. Next, verify if files were successfully copied on the container in the directories /tmp and /usr/share by running the ls command.

Verifying the files if they are successfully copied on the container
Verifying the files if they are successfully copied on the container

Mounting a Storage Volume and Accessing Files with the Docker Volume Command

Up to now, you have learned how to copy files from host to container using two different approaches by using the docker cp command and a DockerFile. This time, let’s learn how to easily share file systems between host and containers using the docker volume command.

Assuming you are still logged into the terminal:

1. Create a volume on Docker host by running docker volume create command. The below command will create a volume named my-vol. sudo docker volume create my-vol

sudo docker volume create my-vol

2. Verify the volume has successfully been created by running the docker volume ls command. docker volume ls command lists the volume. After running the docker volume ls command, you will see my-vol in the VOLUME NAME attribute that confirms that volume is created successfully.

sudo docker volume ls
Listing the docker volumes
Listing the docker volumes

3. Next, run the container using the docker run command.

  • The container named volume_testing uses the nginx: latest image.
  • d flag runs the container in the background and keeps it alive until deleted.
  • v flag mounts the volume my-vol created on Docker host to the container’s destination /app directory.
sudo docker run -d --name volume_testing -v my-vol:/app nginx:latest
Running the Docker command and attaching the volume with the container
Running the Docker command and attaching the volume with the container

4. Verify if the volume my-vol you created previously is mounted properly with the container using the docker inspect command. Docker inspect command provides the information of the container. After running the docker inspect command, it will display all the details of the specified container (volume_testing) details, including the mount details, as shown below.

sudo docker inspect volume_testing

Below, the Image snapshot confirms that the volume (my-vol) you created on the host is successfully mounted with the container’s /app directory.

Verifying the volumes in the container
Verifying the volumes in the container

Conclusion

In this tutorial, you learned different ways of copying files or folders from Docker host to containers, such as using Docker cp command, Docker volume commands, and Dockerfile.

So which approach are you going to use next while copying data from the host to Docker containers?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!