---
title: "How to Copy Files with Docker cp to your Docker Container"
description: "Learn how to use the docker cp command with a great Docker cp example and other methods to copy files to Docker containers in this tutorial!"
canonical: "https://adamtheautomator.com/docker-cp/"
---

# How to Copy Files with Docker cp to your Docker Container

> Learn how to use the docker cp command with a great Docker cp example and other methods to copy files to Docker containers in this tutorial!

Source: https://adamtheautomator.com/docker-cp/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Copy Files with Docker cp to your Docker Container](https://adamtheautomator.com/wp-content/uploads/2021/09/How-to-Copy-Files-from-the-Host-to-your-Docker-Container.jpg)

# How to Copy Files with Docker cp to your Docker Container

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)6 September 20217 min. read

Categories: [DevOps](/category/devops/)

Tags:[Docker](/tag/docker/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Copying Files with the docker cp Command](#copying-files-with-the-docker-cp-command)
*   [Copying Files using DockerFile](#copying-files-using-dockerfile)
*   [Mounting a Storage Volume and Accessing Files with the Docker Volume Command](#mounting-a-storage-volume-and-accessing-files-with-the-docker-volume-command)
*   [Conclusion](#conclusion)

If you need to copy files from the [Docker host](https://docs.docker.com/get-started/overview/) to your [Docker container](https://www.docker.com/resources/what-container), this tutorial is for you.

In this tutorial, you will learn to copy files from the Docker host to a Docker container using various approaches.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Let’s get to it!

## **Prerequisites**

If you’d like to follow along step-by-step, ensure you have the following:

*   A Linux host. This tutorial uses [Ubuntu 18.04.5 LTS](https://releases.ubuntu.com/18.04/).
    
*   Docker installed on the Linux host. This tutorial uses Docker v19.03.11. You can confirm your Docker version by running `docker version`.
    

Related:[How to Install and Use Docker on Ubuntu (In the Real World)](https://adamtheautomator.com/docker-ubuntu/)

*   Any Docker image downloaded and available. [This tutorial uses the latest NGINX Docker image available on Docker Hub](https://hub.docker.com/_/nginx).

## Copying Files with the `docker cp` Command

To start this tutorial, you will learn to copy files from Docker host to the containers using the [`docker cp`](https://docs.docker.com/engine/reference/commandline/cp/) command. The `docker cp` command copies files or folders between a container and the local filesystem of your Docker host and vice versa.

Let’s learn how to use Docker cp command with an example.

1\. Open a terminal on your local machine.

2\. Create a file named _myfile.txt_ using the `touch` command. The _myfile.txt_ will be copied from the docker host to the container.

```bash
touch myfile.txt
```

3\. Execute the `docker run` command. The below [docker run](https://docs.docker.com/engine/reference/commandline/run/) will create a new container in the background. The below command contains three parameters described below:

*   `d` flag that runs the container in the background and keeps it alive until deleted.
*   `p` flag publishes a container’s port `80` to the host on port `80`.
*   `nginx` will be the image that will be used to run the container.

```bash
sudo docker run -d -p 80:80 nginx
```

![Executing the Docker run command](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T195300.252.png)

Executing the Docker run command

4\. Verify if the Docker container is successfully created using the [`docker ps`](https://docs.docker.com/engine/reference/commandline/ps/) command. After you execute the `docker ps` command, you should see a new value generated under the attribute **CONTAINER ID (In the below case `ccae4670f030`)** using the NGINX image that confirms the container is successfully created.

```bash
sudo docker ps
```

![Verifying the docker container by running the docker ps command](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T195422.600.png)

Verifying the docker container by running the docker ps command

Before you execute the Docker cp command, The syntax of the Docker cp command is:

*   `CONTAINER: SRC_PATH` specifies the source path of the container.
*   `DEST_PATH` is the destination path on the host.
*   `CONTAINER: DEST_PATH` is the destination path on the container.

Additionally, you can also add the `options` with the below parameters in the command as follows:

*   Using archive or a – Copies all the user and primary group permissions of the files and folders.
*   Using L – Specifying the L option will allow any symbolic link in the Source Path to be copied to the destination path.

```bash
# Syntax to Copy from Container to Docker Host  
docker cp {options} CONTAINER:SRC_PATH DEST_PATH 
# Syntax to Copy from Docker Host to Container  
docker cp {options} SRC_PATH CONTAINER:DEST_PATH 
```

5\. Next, execute the `docker cp` command. The `docker cp` command will copy the _myfile.txt_ that you created earlier to the containers _/usr/share_ directory. `ccae4670f030` is the container ID to which the _myfile.txt_ will be copied.

```bash
sudo docker cp myfile.txt ccae4670f030:/usr/share
```

6\. Finally, SSH into the running container by running the [`docker exec`](https://docs.docker.com/engine/reference/commandline/exec/) command with `/bin/bash`, which is used as the default shell for user login of the Linux system.

*   The `i` flag indicating you’d like to open an interactive SSH session to the container. The `i` flag does not close the SSH session even if the container is not attached.
*   The `t` flag allocates a pseudo-TTY which much be used to run commands interactively.`sudo docker exec -it ccae4670f030 /bin/bash`

```bash
sudo docker exec -it ccae4670f030 /bin/bash
```

You’ll see below that you’re now connected to the container’s shell when you run `docker exec`.

![SSH into the running container using docker exec command](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T195651.937.png)

SSH into the running container using docker exec command

7\. After logging in to the container, verify if _myfile.txt_ has been copied to the container using the [ls](https://en.wikipedia.org/wiki/Ls) command.

*   `ls` is a command to list computer files in Unix and Unix-like operating systems.
*   `grep` will search for all files or folders, starting with string `my` inside the _usr/share_ directory.

![Verifying the files in the container](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T195807.169.png)

Verifying the files in the container

## Copying Files using DockerFile

In the previous section, you learned how to copy the files into the container by running the Docker cp command. What if you need to copy multiple files in one go? Certainly, running multiple commands becomes an overhead! To solve running multiple cp commands, why not try copying files or folders into containers using Dockerfile with `COPY` commands?

Deploying a container and copying the files/folders using Dockerfile allows you to remove the manual copying steps you did in the previous section. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Let’s create a Dockerfile, run a container from it, and finally copy the files.

1\. Create a folder named ~/_host-to-container-copy-demo,_ then change (`cd`) the working directory to that folder. This folder will contain all of the files you’ll be creating in this demo.

```bash
mkdir ~/host-to-container-copy-demo
cd ~/host-to-container-copy-demo
```

2\. Now, create two text files named _myfile1.tx_t and _myfile2.txt_, copy and paste them into the files, and save them to the _~/host-to-container-copy-demo_ directory.

```powershell
# myfile1.txt
Hello This is my first file !
This is file will be copied in /usr/share directory from Docker host to Docker Container.
```

```powershell
# myfile2.txt
Hello This is my second file !
This is file will be copied in /tmp directory from Docker host to Docker Container.
```

3\. Create another file, copy/paste the below configuration, and save the file as _Dockerfile_ inside the ~/_host-to-container-copy-demo_ directory. When complete, Docker will use this DockerFile to run all the commands necessary to build a new Docker image on top of any base image.

The _DockerFile_ below contains various steps/instructions that will build the new container:

*   `FROM`**–** The [FROM](https://docs.docker.com/engine/reference/builder/#from) instruction initializes a new build stage and sets the Base Image for subsequent instructions.
*   `COPY`**–** [COPY](https://docs.docker.com/engine/reference/builder/#copy) command copies a file from the host machine (Docker host) to the container.

```bash
# Instruction for Dockerfile to create a new image on top of the base image (ubuntu)
# Using the base image ubuntu: latest
FROM ubuntu:latest
# Copying myfile1.txt to the containers /usr/share directory
COPY myfile1.txt /usr/share
# Copying myfile2.txt to the containers /tmp directory
COPY myfile2.txt /tmp
```

4\. Verify all of the required files to build the new image by running the `tree` command. You should see **_Dockerfile, myfile1.txt, and myfile2.txt_** under the ~/_host-to-container-copy-demo_ directory.

![Verifies files and folders using tree command](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200049.699.png)

Verifies files and folders using tree command

5\. Next, build the image by running the `docker build` command. The `t` flag is used to tag the image `updated_ubuntu_image` with the `latest` and`.` allows docker to pick all the necessary files from the present working directory.

```bash
sudo docker build -t updated_ubuntu_image:latest .
```

![Building the Docker image by running the docker build command](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200156.962.png)

Building the Docker image by running the `docker build` command

6\. Now, verify the newly built image `updated_ubuntu_image` by running the `docker images` command. Note the **REPOSITORY** attribute. This attribute is the tag created with the `-t` flag in the previous step.

```powershell
sudo docker images
```

![Repository Attribute](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200244.717.png)

Repository Attribute

7\. Finally, run the docker container using the newly built image by running the `docker run` command. The `-it` flag instructs Docker to allocate a [pseudo-terminal](https://en.wikipedia.org/wiki/Pseudoterminal) connected to the container’s stdin. `bash` provides the default shell for user login of the Linux system.

```powershell
sudo docker run -it updated_ubuntu_image bash
```

You’ll see below that you’re now in the Docker container’s Bash shell.

![Running the container using docker run command. ](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200420.718.png)

Running the container using docker run command.

8\. Next, verify if files were successfully copied on the container in the directories _/tmp_ and _/usr/share_ by running the `ls` command.

![Verifying the files if they are successfully copied on the container](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200614.284.png)

Verifying the files if they are successfully copied on the container

## Mounting a Storage Volume and Accessing Files with the `Docker Volume` Command

Up to now, you have learned how to copy files from host to container using two different approaches by using the `docker cp` command and a _DockerFile_. This time, let’s learn how to easily share file systems between host and containers using the [`docker volume`](https://docs.docker.com/engine/reference/commandline/volume_create/) command.

Assuming you are still logged into the terminal:

1\. Create a volume on Docker host by running `docker volume [create](https://docs.docker.com/engine/reference/commandline/volume_create)` command. The below command will create a volume named `my-vol`. `sudo docker volume create my-vol`

```bash
sudo docker volume create my-vol
```

2\. Verify the volume has successfully been created by running the [`docker volume ls`](https://docs.docker.com/engine/reference/commandline/volume_ls/) command. [docker volume ls](https://docs.docker.com/engine/reference/commandline/volume_ls/) command lists the volume. After running the `docker volume ls` command, you will see `my-vol` in the VOLUME NAME attribute that confirms that volume is created successfully.

```bash
sudo docker volume ls
```

![Listing the docker volumes](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T200837.437.png)

Listing the docker volumes

3\. Next, run the container using the `docker run` command.

*   The container named `volume_testing` uses the `nginx: latest` image.
*   `d` flag runs the container in the background and keeps it alive until deleted.
*   `v` flag mounts the volume `my-vol` created on Docker host to the container’s destination `/app` directory.

```powershell
sudo docker run -d --name volume_testing -v my-vol:/app nginx:latest
```

![Running the Docker command and attaching the volume with the container](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T201002.454.png)

Running the Docker command and attaching the volume with the container

4\. Verify if the volume my-vol you created previously is mounted properly with the container using the [`docker inspect`](https://docs.docker.com/engine/reference/commandline/inspect/) command. [Docker inspect command](https://docs.docker.com/engine/reference/commandline/inspect/) provides the information of the container. After running the `docker inspect` command, it will display all the details of the specified container (`volume_testing`) details, including the mount details, as shown below.

```bash
sudo docker inspect volume_testing
```

Below, the Image snapshot confirms that the volume (`my-vol`) you created on the host is successfully mounted with the container’s `/app` directory.

![Verifying the volumes in the container](https://adamtheautomator.com/wp-content/uploads/2021/09/Untitled-2021-09-05T201113.128.png)

Verifying the volumes in the container

## Conclusion

In this tutorial, you learned different ways of copying files or folders from Docker host to containers, such as using Docker cp command, Docker volume commands, and Dockerfile.

So which approach are you going to use next while copying data from the host to Docker containers?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-cp%2F&text=How%20to%20Copy%20Files%20with%20Docker%20cp%20to%20your%20Docker%20Container)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fdocker-cp%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-cp%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-13.webp)

### [Azure Container Apps: Instant Preview Environments per PR](/build-ephemeral-preview-environments-every-pr/)

Build isolated preview environments for every pull request using Azure Container Apps revision labels and Azure DevOps pipelines with scale-to-zero economics.

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
