How can I copy a file from one folder to another folder within a container in Docker?


In this article, we are going to discuss “How to transfer files as well as directories from the host operating system to the Docker container environment.” The need for all these commands is that many times developer has to provide various types of files to the Docker container, these files are configuration files, security and authentication files, application files, and many more. So, this is a very simple and important task that needs to be performed while creating or after the creation of the Docker container on the Docker daemon. Some commands are also going to be used while creating a Docker image and further creating the Docker container from that image.

Types of Commands

There are two types of commands that can be used on the Docker container to transfer files, one is from the terminal or CLI ( Command Line Interface) and the other is while creating a Dockerfile. We also have some scope of the Docker compose to transfer files to the Docker container.

Using Terminal Commands

For these commands to get implemented on your system, the Docker daemon must be in running status because these commands are the base commands of the Docker client application.

host to container and container to host.

Command 1: Docker cp

First of all, we need to create a test Docker container, here we have used the busybox Docker image. Use this command to create the container in interactive and detached modes.

$docker run -itd --name cont1 busybox



Now the container has been created.

Variation 1 HOST to Container

Suppose we want to transfer a text file to the container that is from HOST to Docker Container.

$nano test.txt


This is a text file that has to be transferred to the Docker container.

Now use the below command to copy this to the “cont1” busybox container.

General syntax of the command: $docker cp [source_path] [container:destination_path]

$docker cp /home/hemant/test.txt cont1:/

Now cross-check if the file was copied to the container. First, get inside the container and then check for the file.

$docker exec -it cont1 sh


Print the file on the terminal.

/#cat test.txt


This is a text file that has to be transferred to the Docker container.


Hence the file was copied successfully. Instead of using the Docker container’s name, we can also use the container ID to copy the file.

$docker ps


21e7886419a1 busybox "sh" 10 hours ago Up 14 minutes cont1


$docker cp /home/hemant/test.txt 21e7886419a1:/

Variation 2: Container to HOST

To copy files from the container to the host, reverse the [source_path] and [destination_path] used in the above example.

Get inside the container and create a file “test2.txt” and transfer this to HOST.

$docker exec -it cont1 sh


/# vi test2.txt



Now exit from the container and return to the HOST terminal and execute the below command to get that “test2.txt” file on the HOST machine.

$docker cp cont1:/test.txt .

Check the file

$cat test2.txt



Hence the copy was successful.

Variation 3: Docker container cp

The command “docker cp” can also be used as “docker container cp”, below we have implemented the same. Nothing needed to be changed, all steps are similar.

$docker container cp cont1:/test2.txt .

Command 2: Docker Volumes

Another and the most used way to get a copy of files from the container. This also helps to create persistent data for the container. List all the docker volumes that exist on the system.

$docker volume ls

Now create your own volume “database” that could be used to connect to any of the containers.

Syntax − docker volume create <name_of_ the_volume>

$docker volume create database

Check if the volume was created.

$docker volume ls


local   volume1
local   database
local   volume2

Now use this volume and connect this to a container using the “-v” tag. General

Syntax − docker run –it –v volume−/container_directory image

$docker run -it --name cont2 -v database:/database busybox

Let us see if the docker volume can be used to store the data to the host from the container. Get inside the container “cont2”, create a text file, and then exit from the container.

$docker exec -it cont2 sh /#cd database /#touch databasefile.txt //exit the container $exit

Now let us create a new container “cont3”, link the same docker volume to this container, and then check if the databasefile.txt is present or not.

$docker run −itd --name cont3 -v database:/database busybox //get inside this container $docker exec -it cont3 sh //change to the database directory /#cd database //list the items /#ls Output: databasefile.txt

Hence the data was saved and copied successfully.

Using Dockerfile

Dockerfile is used to create docker images by adding various layers to it. We have some layers which help to copy or transfer data to the container image either from the host or from the internet.

Command 1: ADD

The ADD command can be executed for different purposes.

Variation 1: To extract compressed files.

General syntax − ADD source_files_location destination_directory_path

By default, if the ADD is given a compressed file this will extract the file and save it on the container image while building the image.

ADD compressed_file.extension /container_folder_name

But the extraction is limited to source files which are stored at the HOST machine only. It does not extract the web-stored compressed files. And also it has limited extension support for extractions.

Variation 2: To copy from the web

The general syntax is the same as mentioned above, instead of the local path we just have to give the remote path location of the files.

ADD <remote_path_or_link_to_file> <destination_folder_on_container_image>


Suppose we want to download the HTML webpage of the Tutorialspoint website to our apache webserver for some test purposes. We use the below ADD layer.

ADD /usr/local/apache2/htdocs/

Now we can run this webpage on our Docker container and perform testing. If the link passed is a compressed file, it will not extract the file but download it to the path mentioned.

Command 2: COPY

This command has no variations, it is simple and implements only one task of copying the mentioned files to the destination. It does not do the extraction as we have seen in the ADD command, and it can not download from the web.

COPY <source_file_path> <destination_on_container_image>

Command 3: RUN

Curl and Wget commands are used to copy files from the web. We will use these commands while preparation of the image using dockerfile.

RUN curl --output <downloadedfile> <url_to_the_web_file>
RUN wget <url_to_the_web_file>


Reasons for copying from local and remote machines are mentioned below.

  • Copying configuration files.

  • Copying the actual code of the application.

  • Copying from the web for testing purposes.

  • Copying from the web for testing purposes.

  • Copying the container data to the host for the sack of persistent data.

Updated on: 11-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started