4 Ways to copy file from localhost to docker container

When working with Docker containers, one of the most common tasks is to copy files from the local host to a container that is already operating.

In this post, we will explore a variety of methods for coping files from a local host to a Docker container that is already in operation.

Table Of Content

  1. Copy file using the Docker CLI command "docker cp":
  2. Copy file using docker bind volume
  3. Copy files and directory with Docker compose
  4. Copy file using Dockerfile "COPY" instruction

1. Copy file using the Docker CLI command "docker cp":

Using the "docker cp" command is the quickest, least complicated, and most effective method for copying files into a Docker container that is already operating.

Using this command, we are able to copy files and directories from our local machine to inside a container's filesystem.

The following is an example of correct syntax for the "docker cp" command:

1# Replace the local file path which you want to copy
2# Also replace the container file path where you want to copy
3
4docker cp /path/to/local/file.txt mycontainer:/path/to/container/file.txt

Here are some screenshot which will help you to understand the above example -

1. First check your container is running before you start the copy.

1# Check the docker running status and copy the container ID
2
3docker ps -a

check the docker container running status

2. Check the location inside the docker container - In the previous step you got the container id, now use the following docker exec -it command to verify the location inside the docker container where you want to copy the file.

1# Replace the container id with your container ID
2docker exec -it 8c06902e8efc sh 

docker exec -it into your docker container

I would like to copy my test-file.txt to /usr directory inside the running docker container. Let's check the path where we want to copy the file.

Copy file to /usr directory inside docker container

3. Run the docker cp command to copy the file Now we know the container-id as well as the destination directory where we want to copy the file.

Let's run the following command -

1docker cp test-file.txt 8c06902e8efc:/usr

Here the screenshot of how I copied the file to docker container and verified the file by login into the docker container -

Verify the copied file by logging into the running docker container


2. Copy file using docker bind volume

Utilizing a bind volume is yet another option for moving files from the local host to a container that is currently running Docker.

Through the use of bind volume, we are able to mount a directory on the local host as a volume within the container. Any alterations that are done in the container will be mirrored in the directory on the local machine, and vice versa.

When utilizing a bind volume, it is necessary for us to not only supply the path to the local directory, but also the path to the container directory that we wish to mount.

When we execute the container, we can accomplish this by using the "-v" option from the command line.

Here I have documented the steps from scratch from creating the volume till all the way to copy the file -

1. Create volume - The first step would be to create a volume before we transfer/copy the file.

1docker volume create test-volume 

2. Verify the volume - After creating the volume let's verify it by running the following command.

1# List the available docker volume 
2
3docker volume ls 

docker list volume

1# Inspect the docker volume 
2
3docker inspect volume test-volume 

docker inspect volume

3. Copy the file to mounted docker volume- Now here is the command to copy the file to the mounted volume .i.e. test-volume

1# Docker command to copy file to mounted volume
2# Please refer to the break down of the command below
3
4docker run -itd --name httpd --mount source=test-volume,target=/my-test-vol alpine

Let's break down the command and explain each part in detail:

  • "docker run": This command tells Docker to create and run a new container.
  • "-itd": These are three options that modify the behavior of the container. "-i" specifies that the container should run in interactive mode, "-t" specifies that a pseudo-tty should be allocated, and "-d" specifies that the container should run in detached mode, i.e., in the background.
  • "--name httpd": This option gives the container a name, which in this case is "httpd".
  • "--mount source=test-volume,target=/my-test-vol": This option specifies that a named volume called "test-volume" should be mounted to the container at the path "/my-test-vol". The "source" parameter specifies the name of the volume to mount, and the "target" parameter specifies the path inside the container where the volume should be mounted.
  • "alpine": This is the name of the Docker image that should be used to create the container.

3. Copy files and directory with Docker compose

Docker Compose is an extremely effective tool for managing Docker applications that span several containers.

It gives you the ability to define and execute several Docker containers as a single application and provides a straightforward method for managing the configuration, networking, and storage of those containers. In this article, we will investigate the process of using Docker Compose to copy a single file or many files into a container.

1. Using Docker Compose to Copy a Single File into a Container

To use Docker Compose to transfer a single file into a container -

  1. We must first define a service for the container within the Compose file
  2. Then use the "volumes" directive to indicate the file that should be copied into the container.

Take a look at this example: The following is an example of a compose file that will copy a single file with the name "config.ini" to a container named "web":

1# Copy single file inside docker container
2
3version: '3'
4services:
5  web:
6    image: nginx
7    volumes:
8      - ./config.ini:/etc/nginx/conf.d/config.ini

2. Copying Multiple Files to a Container Using Docker Compose

Docker Compose allows us to establish a distinct volume for each file or directory that we want to transfer into a container.

This allows us to copy several files at once into a container.

The following is an example of a file that may be written in Compose that would copy two directories and one file into a container called "web":

 1# Copy multiple files and directory inside docker container
 2
 3version: '3'
 4services:
 5  web:
 6    image: nginx
 7    volumes:
 8      - ./config.ini:/etc/nginx/conf.d/config.ini
 9      - ./html:/usr/share/nginx/html
10      - ./logs:/var/log/nginx
11 

In the above example, we will define a service that we will refer to as "web" and that will make use of the "nginx" image.

After that, we use the "volumes" directive to define three different volumes. The first volume mounts the file "config.ini" that is located on the host computer to the directory "/etc/nginx/conf.d/config.ini" that is located within the container.

The second volume "mounts" the directory "html" located on the host to the location **"/usr/share/nginx/html"** located within the container.

The third volume "mounts" the directory "logs" located on the host to the directory "/var/log/nginx" located within the container.

When the container is started with Docker Compose, every file and directory that is provided in the volumes directive will be copied to the container. This will take place before the container is started.


4. Copy file using Dockerfile "COPY" instruction

When constructing a Docker image, you can find yourself in a situation where you need to incorporate certain files into the image. The "COPY" directive in a Dockerfile provides an effective and flexible method for copying files from the host into the Docker image.

In this section, we will discuss how to use the "COPY" instruction to copy files into a Docker image.

1. An Explanation of the COPY Instruction's Syntax

The following is an example of the syntax for the "COPY" instruction:

1# Dockerfile copy command
2
3COPY <src> <dest>
  1. : The "src" parameter allows the user to specify the host file or directory from which content should be transferred into the image.
  2. : The "dest" argument identifies the location within the image's file system that the file or directory should be transferred to after it has been copied.

Here is a Dockerfile to copy a single file to docker container -

1# Copy only single file - app.py
2
3FROM python:3.9-alpine
4
5WORKDIR /app
6
7COPY app.py /app

Here is a Dockerfile to copy a Multiple Files to docker container -

1# Copy mulitple files present under src directory - src/*.py
2
3FROM python:3.9-alpine
4
5WORKDIR /app
6
7COPY src/*.py /app 

Here is a Dockerfile to copy a Directory to docker container -

1# Copy directory src into docker container
2
3FROM python:3.9-alpine
4
5WORKDIR /app
6
7COPY src /app

Posts in this series