Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Copy Files with Docker cp to your Docker Container

How to Copy Files with Docker cp to your Docker Container

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 how to copy files from the Docker host to a Docker container using several approaches.

Not a reader? Watch this related video tutorial! Don’t see the video? Make sure your ad blocker is turned off.

Let’s get to it!


If you want to go step by step, make sure you have the following:

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

Related: How to Install and Use Docker on Ubuntu (in the Real World)

  • Any downloaded and available Docker image. This tutorial uses the latest Docker NGINX image available on Docker Hub.

Copy files with the docker cp command To start this tutorial, you will learn how to copy


from the Docker host to containers using the docker cp command. The docker cp command copies files or folders between a container and the local file system of your Docker host and vice versa.

Let’s learn how to use the 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.

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

d flag that runs the container in

  • the background and keeps it active until it is deleted
  • .

  • Flag P publishes port 80 of a container to the port 80 host
  • .

  • Nginx will be the image that will be used to run the container.

4. Check if the Docker container was created correctly using the docker ps command. After running the docker ps command, you should see a new value generated under the CONTAINER ID attribute (in the following case ccae4670f030) using the NGINX image confirming that the container has been created successfully.

Before running the

Docker cp command, the Docker cp command syntax

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


  • CONTAINER: DEST_PATH is the destination path
  • in the container. In addition, you can also add the options with the following parameters in the command as follows: Use of archive or a – Copies all user and parent group permissions of 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.

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

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

  • The i flag indicates that you want to open an interactive SSH session in the container. The i flag does not close the SSH session even if the container is not connected.
  • The t flag assigns a pseudo-TTY that will be used a lot to execute commands interactively.sudo docker exec -it ccae4670f030 /bin/bash

You will see below that you are now connected to the container shell when you run docker exec.

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

  • ls is a command for listing computer files on Unix and Unix-like operating systems
  • .

  • grep will search for all files or folders, starting with string my inside the usr/share directory.

Copy 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 at once? Certainly, executing multiple commands becomes an overhead! To solve the execution of multiple cp commands, why not try copying files or folders into containers using Dockerfile with COPY commands?

Deploying a container and copying the files or folders using Dockerfile allows you to eliminate the manual copy steps you performed in the previous section. A Dockerfile is a text document that contains all the commands that a user can 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 the files you will create in this demo.

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

3. Create another file, copy/paste the following settings, and save the file as Dockerfile inside the ~/host-to-container-copy-demo directory. When finished, Docker will use this DockerFile to run all the commands needed to create a new Docker image on top of any base image.


following DockerFile contains several steps/instructions that will compile

the new container: FROM: The

  • FROM statement initializes a new build stage and sets the base image for subsequent statements
  • .

  • COPY: The COPY command copies a file from the host Docker to the container.

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

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

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

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

You will see below that it is now in the Bash shell of the Docker container


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

Mount a

storage volume and file access with the Docker Volume


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

Assuming you are still logged into the terminal:

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

2. Verify that the volume was created correctly by running the docker volume ls command. The Docker Volume ls command lists the volume. After running the docker volume ls command, you will see my-vol in the VOLUME NAME attribute confirming that the volume was successfully created.

3. Then run the container using the docker run command.

  • The container named volume_testing uses the latest nginx: image
  • .

  • D flag runs the container in the background and keeps it active until it is deleted
  • .

  • v mounts the my-vol volume created on the Docker host in the target /app directory of the container.

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

The image snapshot then confirms that the volume (my-vol) that you created on the host was successfully mounted with the container’s /app directory.


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

what approach are you going to use next when copying data from the host to Docker containers?

Contact US