Docker

How to Copy Files Between Host and Docker Container

The docker copy command enables you to copy files from host to container and from container to the host. Learn how to use this command.

Abhishek Prakash
Abhishek Prakash

Table of Contents

Docker cp command is a handy utility that allows to copy files and folders between a container and the host system.

If you want to copy files from your host system to the container, you should use docker cp command like this:

docker cp host_source_path container:destination_path

If you want to copy files from the container to the host system, use this command:

docker cp container:source_path host_destination_path

That may look a bit intimidating so let me explain it to you by some practical examples.

Docker cp command examples

Docker Copy Files Between Host Container

In my tutorial, I have installed Docker on Ubuntu. But the steps are the same for all distributions running docker.

I hope you know how to run a docker container because you’ll need a running container.

List your running containers first using docker ps command:

[email protected]:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8353c6f43fba        775349758637        "bash"              8 seconds ago       Up 7 seconds                            ubu_container

You need to know either the container ID or the container name. In my case, the docker container name is ubu_container. and the container ID is 8353c6f43fba.

If you want to verify that the files have been copied successfully, you can enter your container in the following manner and then use regular Linux commands:

docker exec -it ubu_container bash

1. Copy files from host system to docker container

Copying with docker cp is similar to the copy command in Linux.

I am going to copy a file named a.py to the home/dir1 directory in the container.

docker cp a.py ubu_container:/home/dir1

If the file is successfully copied, you won’t see any output on the screen. If the destination path doesn’t exist, you would see an error:

[email protected]:~$ sudo docker cp a.txt ubu_container:/home/dir2/subsub
Error: No such container:path: ubu_container:/home/dir2

If the destination file already exists, it will be overwritten without any warning.

You may also use container ID instead of the container name:

docker cp a.py 8353c6f43fba:/home/dir1
Docker Run vs Start vs Create: Difference Explained
For a docker beginner, terms like docker start, docker run and docker create could be confusing. This article explains the difference with examples.

2. Copy files from docker container to host system

The docker cp command is run in the host system only. You cannot run it in the container.

Even if you want to copy files from docker container to the host system, you need to be in the host system and use the command in the following manner:

sudo docker cp ubu_container:/home/dir1/new.txt  /home/abhishek

3. Copy directories between container and host system

If you want to copy directory in Linux using cp command, you’ll have to use the recursive option -r.

But in docker cp command, you need no such options. The same command works for copying both files and folders.

docker cp /home/abhishek/my_dir ubu_container:/home
sudo docker cp ubu_container:/home/my_dir  /home/abhishek

4. Copy files and directories with original attributes

You can use the archive mode while copying with option -a. With this, you can copy all the file permissions, UID and GID of the original file.

docker cp -a host_source_path container:destination_path
docker cp -a container:source_path host_destination_path

That’s about it. You see how easy it is to copy files between host and container using the docker cp command.

If you have got some questions or suggestions, do let me know in the comment system.



Join the conversation.