We can use the Docker build command to build Docker images using a build context. The build context contains all the files which are required to create your containerized application environment. This includes the Dockerfile to build the Docker images, source code of the application, Dockerignore files, all the files, and directories that you want to be there beforehand in the container when you run it.
However, it often happens that you might want to copy some files from the container to the host machine. For example, if you are working on an application inside a Docker container and you have generated some log files or outputs inside the container. You might want to share the results with your team. In such a case, you will have to get access to those files from the container to the host machine.
There are two ways to do this. The first one is to mount directories in your host machine as volumes. However, for sharing simple files or directories, it might be unnecessary to create volumes. The second method is by using the Docker cp command to copy files from Docker containers to the host machine. Let’s discuss how to do so.
We can use the Docker cp command to copy files and directories from the source path to the target path. We can use this command to copy files from local machines to Docker containers and vice-versa. The container which is involved in this process can be both stopped or running. By default, the Docker cp command will assume that all the paths mentioned inside the container are relative to the container’s root directory (/).
This means that even if we don’t supply a forward slash, it is optional. Hence, the paths /user/app/folder1 and user/app/folder1 are identical. However, the paths that we specify for local machines can be both relative as well as absolute. The path in the host machine is relative to the current working directory.
We can specify both paths for both files and directories in the source and destination paths. The syntax of the Docker cp command to copy files from Docker containers to host machines is -
$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
Please note that we are not allowed to copy some sensitive files such as system files. These include resources under /dev, /proc, /sys, tmpfs, and certain mounts in the container that are created by the user. A workaround to this is by manually executing tar commands inside Docker containers using the Docker exec command.
Let’s check out an example to create a file inside a Docker container, include some content in it, and copy it into the host machine using the Docker cp command.
First, let’s create an Ubuntu container and access its bash.
$ docker run -it --name=myubuntu ubuntu bash
In the above command, we have used the Docker run command to pull and run an Ubuntu container. We have used the i (interactive) and t (pseudo-TTY) options along with the Docker run command to access the container in interactive mode. Next, we have specified the image name and the command that we need to run, which is the bash command, since we want access to the container environment.
Now that we have access to the container’s bash, we can use the echo command to create a file with some content inside the container.
# echo "Welcome to TutorialsPoint" > TutorialsPoint.txt
You can verify the creation of the file by listing the files and using the cat command to list the contents.
# ls # cat TutorialsPoint.txt
Now, open another terminal. You can either keep this container running or exit from it. In the new terminal, let’s execute the Docker cp command to copy the TutorialsPoint.txt file from the ubuntu container to the host machine.
$ docker cp myubuntu:TutorialsPoint.txt /home/user/Desktop/
In the above Docker cp command, we have specified the name of the container and the path of the file inside the container separated by a colon. We have also specified the destination path inside the host machine. After executing this command, the file will be copied to the destination path.
There are several methods to copy files from Docker containers to host machines. You can use mounted volumes, save the image as tarball files, and various other techniques. But if you want to copy small files or directories, the best possible way is to use the Docker cp command. It’s quite fast and easy to use. Except for some important system files, you can copy any file or directory from the container.
In this article, we discussed how to use the Docker cp command to do so along with it’s syntax and a complete end-to-end example.