How to copy files from host to Docker container?

If you have a Docker container running and you want to copy files from the host machine to the Docker container, there are several ways to do it. One way to do this is by using the ADD or COPY instructions inside a Dockerfile and passing the paths of the files that you want to be in your container when you start it. But what happens if you already have a container running? It's not feasible to build the same image again and again just for a small file to be included in it.

To avoid this fiasco, Docker allows us with several different methods and commands to copy files from the host machine to the container once we have created the container from the image. In this article, we will discuss different methods to do so. Let's check out each of them.

Method 1 − Using the Docker cp Command

The Docker cp command can be used to copy files and directories from the host machine to a container and vice-versa. To copy one single file from the host to container, you can use the command below.

$ docker cp file.txt container-name:/path/to/copy/file.txt

You can use the container ID as well. To find the name of the container or ID, you can list all the containers.

$ docker ps -a

If you want to copy multiple files from a folder in the host machine to the container, you can use the following command.

$ docker cp src/directory/. container-name:/target/directory/location

Method 2 − Mounting a Host Directory

Another way to copy files from host to container is by mounting a directory from the host machine to the Docker container while creating the container. This way, you can copy any file or directory directly by copying the file to the mounted folder in the host machine. Let's see how to do so.

$ docker run -d -v /path/to/directory/in/host:/mount --name=<container-name> <image-name>

You can run this command in the host machine to run a container and use the -v option to mount a directory in the host machine to the container. Any file you copy in this directory will be accessible to the mounted directory inside the container.

Now, let's try to access the bash of the container by running the bash command using the Docker exec command.

$ docker exec -it <container-name> bash

Now that you have access to the bash of the container, you can run the command below to copy the files.

$ cp /mnt/sourcefile /path/to/destfile

Method 3 − Using the Cat Command

Another fairly simple way of copying the files while creating the container is by using the cat command as an argument to the Docker run command.

$ docker run -i ubuntu /bin/bash -c 'cat > filetobecopied' < filetobecopied

If you already have a Docker container running, you can use the Docker exec command with the same cat command.

$ docker exec -it <container-name> bash -c 'cat > /path/to/docker-container/file.txt' < /path/to/host-machine/file.txt

Method 4 − Using the tar Command

You can also use the tar command to convert the file into tarball file and pipe it with the Docker exec command. The command below copies the file.txt file to the /usr directory inside the container.

$ tar -c file.txt | docker exec -i docker-container /bin/tar -C /usr -x

Comparison of Methods

Method Best Use Case Advantages Disadvantages
Docker cp One-time file copying Simple, works with running containers Not suitable for frequent updates
Volume Mounting Shared directories, frequent updates Real-time synchronization Must be set during container creation
Cat Command Small text files Works without extra tools Limited to text files, complex syntax
Tar Command Multiple files, archives Preserves file permissions More complex, requires tar knowledge

Conclusion

In this article, we discussed 4 different methods to copy files from the host machine to the Docker container. The docker cp command is the most straightforward approach for one-time file transfers, while volume mounting is ideal for shared directories that need frequent updates. Choose the method that best fits your specific use case and workflow requirements.

Updated on: 2026-03-17T09:01:38+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements