How to copy files to and from Docker containers?

By | October 5, 2021

When we work on a Docker project, we need to deal with multiple Docker containers running on our systems and all of their filesystems. This can be quite a hectic task since all of them have to be managed using the Docker command-line tool. Moreover, after you have built the Docker containers using the Docker image, you might need to get hold of some files from your Docker containers to your local machine or other containers. Or it can be the other way around.

The first option is that every time you want to copy a file or a directory or any other content for that matter, from the local machine to Docker containers, you can build the image again and again. This can be quite costly especially if the size of the image is large. The second solution is you can mount a directory inside Docker containers and use it as volumes. You can even mount the same volume to multiple containers to share files. And the third option is to use the Docker copy command to directly copy files and directories to and from Docker containers.

Vamware

In this practical guide, we will discuss how to use the Docker copy command with some practical examples. So without any further ado, let’s get started.

How to use the Docker cp command?

We can use the Docker cp command or utility to copy files, directories from the source path to the destination path. Both the paths can be either in your local machine or in your Docker container depending upon the use-case. Let’s see the general syntax for both commands.

To copy files or directories from container to local machine, we can use –

$ docker cp [OPTIONS] CONTAINER:<Source Path> <Destination Path>|-

And to copy contents from local system to Docker containers, you can use –

$ docker cp [OPTIONS] <Source Path>|- CONTAINER:<Destination Path>

We can also use the – instead of the source or destination path to stream an archive file or tarball file directly from standard input or output. Moreover, it does not matter whether the container is stopped or running. You can copy files in both the scenarios.

Please note that the paths that we provide to the Docker cp command are always relative to the root directory (/) of the Docker container. This means that it is optional to mention the initial forward slash. The ownership of the copied files will be set to the current user of the container and the primary group.

Any one of the following scenarios can take place.

  • If the SOURCE_PATH is a file –
    • And the DESTINATION_PATH does not exist.
      • In this case, a file of the same name is automatically created at the destination.
    • The DESTINATION_PATH trails with a slash (/) and does not exist.
      • In this case, it throws an error that the destination directory must exist,
    • The DESTINATION_PATH is a file.
      • The destination file is overwritten.
    • The DESTINATION_PATH is a directory.
      • The source file is copied with the same name.
  • If the SOURCE_PATH is a directory –
    • And the DESTINATION_PATH has no existence.
      • In such an event, the DESTINATION_PATH is automatically created as a directory and the entire content of the SOURCE_PATH is copied as it is.
    • The DESTINATION_PATH is a file.
      • In this case, it will throw an error that the process can not copy a directory content to file.
    • The DESTINATION_PATH is a directory.
      • If the SOURCE_PATH has no trailing /., the directory mentioned in the SOURCE_PATH is copied directly.
      • If the SOURCE_PATH does not end with a trailing /., only the content of the source directory is copied.

Let’s discuss examples where we copy files to and from Docker containers.

1. Copying content from Docker containers to Local Machine.

The first step is to create a Docker container. Let’s create an ubuntu container called myubuntu and access the bash using the interactive mode. We can use the Docker run command to do so.

$ docker run -it --name=myubuntu ubuntu bash

$ docker run -it --name=myubuntu ubuntu bash

Next, let’s create a file inside the container called “techgeekbuzz.txt”. We will use the echo command to create the file and insert some content inside it.

$ echo “Welcome to techgeekbuzz” > techgeekbuzz.txt

$ echo “Welcome to techgeekbuzz” > techgeekbuzz.txt

Let’s list all the files to verify.

$ ls

$ ls

Next, in another terminal, we can issue the Docker cp command mentioned below to copy the file to a directory in our local machine.

$ docker cp myubuntu:/techgeekbuzz.txt ~/Documents

$ docker cp myubuntu:/techgeekbuzz.txt ~/Documents

We can see that the file has been copied. Let’s print the content of the file.

$ cat ~/Documents/techgeekbuzz.txt

$ cat ~/Documents/techgeekbuzz.txt

2. Copying content from local machine to Docker containers.

Let’s create a file in our local machine with some content inside it.

$ echo "Welcome to TechGeekBuzz Docker Tutorial" > ~/Documents/tgb.txt

$ echo "Welcome to TechGeekBuzz Docker Tutorial" > ~/Documents/tgb.txt

Now, let’s run the same Docker container called myubuntu once again.

$ docker exec -it myubuntu /bin/bash

$ docker exec -it myubuntu /bin/bash

Now that we have access to the bash of the container, open another terminal and execute the Docker cp command below to copy this file inside the container.

$ docker cp ~/Documents/tgb.txt myubuntu:/tgb.txt

$ docker exec -it myubuntu :bin:bash

If we list all the files, we will find that the file has been copied. Let’s verify the same by listing all the files inside the Docker container.

$ ls

$ ls

Let’s print the file and verify the content.

Verify the Content

Wrapping Up!

To sum up, in this comprehensive tutorial, we have discussed one of the most handy tricks to copy files and directories from Docker containers to local machines and vice-versa. This will help us to avoid building the Docker images again and again for simply copying processes. We discussed the different scenarios which can arise depending upon the format of the source and destination paths that we specify.

Next, we discussed the commands that allowed us to copy the files to and from Docker containers along with practical examples. The Docker cp command is one of the most important skills to grab when it comes to managing the file systems of multiple Docker containers at once.

We certainly hope that this guide helped you to get hands-on experience working with the Docker cp command.

Happy Learning!

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *