How to Copy Files From Host to Docker Container

Isaac Tony Feb 02, 2024
  1. Use docker cp to Copy Files From Host to Docker Container
  2. Use docker volume to Copy Files From Host to Docker Container
How to Copy Files From Host to Docker Container

This article will discuss and demonstrate the methods we can use to transfer files from our host machine to a running container in Docker.

Use docker cp to Copy Files From Host to Docker Container

The docker cp command is one of the easiest ways that we can use to copy files and directories from our host machine to a Docker container. We will be using Ubuntu 20.4.5 and the latest version of Docker at the time, which is v 19.03.




  • PATH_HOST specifies the source path where the file we want to copy is located on the host.
  • PATH_TO_CONTAINER is the destination path where we want to store the file on the container.


We will start by creating our target container. We’ll be using the official Nginx image for this article, which we can pull through the Docker desktop or the terminal by running the command below.

$ docker pull nginx


Using default tag: latest
latest: Pulling from library/nginx
5eb5b503b376: Pull complete
1ae07ab881bd: Pull complete
78091884b7be: Pull complete
091c283c6a66: Pull complete
55de5851019b: Pull complete
b559bad762be: Pull complete
Digest: sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Status: Downloaded newer image for nginx:latest

We use the following command to verify that we successfully pulled the image from the registry.

$ docker images


nginx        latest    c316d5a335a5   2 weeks ago   142MB

We can now create a container. Using the run command, we will create a container and deploy it in the detached mode to listen on external port 8080 and internal port 80.

The command below will create a container using the Nginx base image.

$ docker run -d -p 8080:80 nginx

We can verify that we have successfully created a new container by running the docker ps -a command. This will list all containers we made recently and in the past, together with their container IDs and respective images.

We can execute this as shown here.

docker ps -a


CONTAINER ID   IMAGE   COMMAND                  CREATED              STATUS              PORTS                  NAMES
0c57de10362b   nginx   "/docker-entrypoint.…"   About a minute ago   Up About a minute>80/tcp   lucid_booth

Before using the docker cp command, we need to create a file that we will be copying from the host to the container we just created.

We will create a file named new_file.txt and store it in the home directory using the’ touch’ command.

Here is the command to do that.

$ touch new_file.txt

We can view the directory to ensure that we have successfully created the file using the ls command as shown here.

isaac@DESKTOP-HV44HT6:~$ ls

Of course, new_file.txt is an empty file; however, we want to focus on using the docker cp command to move it from the host to the container we created earlier.

We then copy this file into the container we created.

$ docker cp new_file.txt 0c57de10362b:/usr/share

Just like that, we have copied new_file.txt into the container with ID 0c57de10362b under the folder /usr/share.

We can ascertain this by getting into the running container through an interactive SSH session using the command docker exec and the -it flag to run commands interactively.

$ docker exec -it 0c57de10362b /bin/bash

Now that we have logged into the container, we want to list files to verify that we have copied this file into this container. We can use the ls command again but specify the folder we want to access.

root@0c57de10362b:/# ls -lh /usr/share/


viewing a file copied into the docker container from the host

Similarly, we can also copy a directory containing many files by simply changing the location of the files to read from the directory, as shown below.

$ docker cp src/directory/. container-id:/target_location/directory

The docker cp command, although easy to use, has some limitations. Its syntax is different from the Unix cp command and only supports a limited number of tags.

Also, this can only be used to copy files from the host to a container and not between containers.

Use docker volume to Copy Files From Host to Docker Container

A better approach for copying files between containers is through docker volume.

We can attach volumes containing files that we want to copy to a container during the container creation process using either of the two flags, -v or -mount flag.

To use volumes in Docker, we first create one using the docker volume command, as shown below.

$ docker volume create volume_one

Using docker volume ls, we can verify that we have successfully created the volume. This will list the names of the volumes created now and in the past and the driver’s name.

isaac@DESKTOP-HV44HT6:~$ docker volume ls
local     volume_one

Now that we have created a volume, the next step involves creating a new container using the run command and attaching the volume we created using the -v flag.

The -v flag allows us to attach the new volume to the container we create. We will make this container using the latest version of the Nginx base image.

$ docker run -d -v volume_one:/app nginx:latest

We can return low-level information about Docker objects using the Docker inspect command. We will investigate whether our volume was successfully attached to the container we created.

Since we did not assign any name to this container, we will use the container ID to inspect it. We will use the most recent container ID and not the earlier one.

$ docker ps -a


CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS             PORTS                  NAMES
f2457d3eb8fe   nginx:latest   "/docker-entrypoint.…"   4 minutes ago       Up 4 minutes       80/tcp                 brave_kirch
0c57de10362b   nginx          "/docker-entrypoint.…"   About an hour ago   Up About an hour>80/tcp   lucid_booth

We can now use this container ID to inspect the container.

$ docker inspect f2457d3eb8fe


Inspecting a mounted volume in a docker container

One of the advantages of the docker volume command is creating a single directory shared with multiple containers.

Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.


Related Article - Docker Container