How to copy files from host to Docker container?

DockerOperating SystemOpen Source

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 copied 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

To sum up, in this article, we discussed 4 different methods to copy files from the host machine to the Docker container. We saw how to copy files from the host to a running container as well as a container that has not even been created. We used tools such as Docker cp, Docker exec, etc. to achieve this task. The best possible way is to use the traditional Docker cp command. Based on your requirements and use-case, you can choose the best possible way to copy the files.

raja
Published on 06-Aug-2021 11:52:45
Advertisements