> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Copy Files Between Host and Docker Container
- URL: https://linuxhandbook.com/docker-cp-example/
- Published: 2019-12-01T11:44:46.000Z
- Updated: 2026-09-08T17:55:25.000Z
- Description: 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.
- Author: Abhishek Prakash
- Tags: Docker Tutorials, #cta-docker-course

*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

In my tutorial, I have [installed Docker on Ubuntu](https://linuxhandbook.com/install-docker-ubuntu/). But the steps are the same for all distributions running docker.

I hope you know [how to run a docker container](https://linuxhandbook.com/run-docker-container/) because you’ll need a running container.

[List your running containers](https://linuxhandbook.com/list-containers-docker/) first [using docker ps command](https://linuxhandbook.com/docker-ps-command/):

```
abhishek@linuxhandbook:~$ 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](https://linuxhandbook.com/cp-command/).

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:

```
abhishek@linuxhandbook:~$ 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 ExplainedFor a docker beginner, terms like docker start, docker run and docker create could be confusing. This article explains the difference with examples.![](https://linuxhandbook.com/assets/icon-192x192.png?v=87ed9de67f)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2020/06/docker_run_start_create.jpg)](https://linuxhandbook.com/docker-run-vs-start-vs-create/)

### 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](https://linuxhandbook.com/copy-directory-linux/) using cp command, you’ll have to use the recursive option -r.

But in [docker cp command](https://docs.docker.com/engine/reference/commandline/cp/?ref=linuxhandbook.com), 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](https://linuxhandbook.com/linux-file-permissions/), [UID](https://linuxhandbook.com/uid-linux/) 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](https://linuxhandbook.com/self-host-comment-system/).