How to Copy Files From Docker Container to Host

Isaac Tony Feb 02, 2024
  1. Use docker cp to Copy Files From Docker Container to Host
  2. Use Docker Mounts to Copy Files From Docker Container to Host
  3. Use COPY to Copy Files From Docker Container to Host
How to Copy Files From Docker Container to Host

Before Docker 1.8, we could only copy files from the container to the host. However, as containers became even more popular, copying files to and from containers has become necessary.

One of the common reasons we may want to copy files from a docker container is to share log files with our team members. However, copying sensitive information such as resources under /dev, /proc, /sys, tmpfs is not allowed.

This article will discuss how to copy files from a Docker container utilizing a DockerFile, the docker cp command, and mounting directories to the host machine as volumes.

This requires installing and configuring Docker and a Docker container correctly. This will work in both Linux and WSL environments.

Use docker cp to Copy Files From Docker Container to Host

The docker cp command is one of the easiest ways that we can use to copy files and directories as well from our host machine to a docker container. We will be using Ubuntu 20.4.5 and the latest version of Docker at the time, which is 19.03.

Syntax:

$ docker cp [OPTIONS] CONTAINER: SRC_PATH DEST_PATH|-

Where:

  • SRC_PATH specifies the source path where the file we want to copy is located on the host.
  • DEST_PATH is the destination path to store the file on the container.

Using this command, we can copy files to and from the container by interchanging the parameters SRC_PATH and DEST_PATH. One advantage of this command is that we can copy both files and directories to and from running or stopped containers.

The docker cp command resembles the Unix cp -a; therefore, it means it also copies directories recursively. In addition, the command also assumes that all paths to the container are relative to the root directory.

We will start by first creating our target container; therefore, we also need an image. We’ll be using the official Ubuntu image available in the Docker registry.

$ docker pull ubuntu

We’ll then create a docker container in detached and interactive mode. This allows our container to run in the background.

$ docker run -it -d ubuntu
e10b4a7bbc59131d30492a87dd70b8ea74fa686ed5036ec918b6596fc83efe2b

Using the exec command, we will run an interactive SSH session using the command docker exec and the -it flag. This will allow us to run commands interactively within the container.

To create a simple text file, we will navigate to the /usr/share folder.

We will start by entering into the container using the respective container id.

$ docker exec -it e10b4a7bbc59 /bin/bash
root@e10b4a7bbc59:/#

We will navigate the folder /usr/share/ within the container to create our text file.

$ docker exec -it e10b4a7bbc59 /bin/bash
root@e10b4a7bbc59:/# cd usr
root@e10b4a7bbc59:/usr# cd share
root@e10b4a7bbc59:/usr/share#

We will create a file named new_file.txt using the touch command. We will shortly copy this file from this container to our host.

Therefore it is good to take note of the directory where the file is stored since we will use this location when copying it to the host.

root@e10b4a7bbc59:/usr/share# touch new_file.txt

To verify that we have created the file, run the ls command to list all files in this directory, as shown below.

root@e10b4a7bbc59:/usr/share# ls
adduser      bash-completion  debianutils  dpkg  keyrings  man           pam          polkit-1
apport       bug              dict         gcc   libc-bin  menu          pam-configs  sensible-utils
base-files   common-licenses  doc          gdb   lintian   misc          perl5        tabset
base-passwd  debconf          doc-base     info  locale    new_file.txt  pixmaps      terminfo

Now that we have successfully created a file within our container, we want to copy this file from this container to our host - exit from the container into the main shell using the exit command.

We will run the docker cp command in the terminal and copy the new_file.txt to a directory in the host machine. However, we first need to take note of three important things:

  • The container id;
  • The location of the target file in the container;
  • The target directory in the host machine where we’ll copy the file.
$ docker cp e10b4a7bbc59:/usr/share/new_file.txt ./new_file.txt

We have successfully copied the new._file.txt to the host machine by executing that command. We can verify this by running the ls command in that particular folder.

isaac@DESKTOP-HV44HT6:~/isaac$ ls
adduser      bash-completion  debianutils  dpkg  keyrings  man     `new_file.txt`  pixmaps         terminfo
apport       bug              dict         gcc   libc-bin  menu    pam           polkit-1
base-files   common-licenses  doc          gdb   lintian   misc    pam-configs   sensible-utils
base-passwd  debconf          doc-base     info  locale    my-app  perl5         tabset

Use Docker Mounts to Copy Files From Docker Container to Host

We can also use docker mounts to copy files from the docker container to the host.

Bind mounts are one of the earliest solutions that allow us to persist data through mounting our docker containers to directories on our host system.

Bind mounts allow us to reference an exact directory by referring to the absolute file path of the target directory. Besides copying files from the container to the host, we can also create services that depend on directories on the host system.

To create a docker mount, we can use either the -v or --mount flag when creating the docker container. In addition to this flag, we will also specify the directory on the host machine we want to map to a directory on the docker container.

This method is often preferred because we can create multiple mounts inside a docker container. We can also create directories to transfer files from several containers.

To mount our directory to the docker container, we must specify the following fields that should always be in the correct order separated by a colon. These include:

  • The path to the directory on the host machine that we want to mount;
  • The path to the directory in the container where we should mount this directory;
  • Other optional options such as ro specify the read-only mode.

Syntax:

docker run -t -i -v <host_dir>:<container_dir

In the example below, we’re using the Ruby official images to create a container and mount a local directory to a directory on the container. Once this is done, we can easily copy files from the container to the local directory and vice versa.

$ docker run -it -v $HOME/Desktop/new_container:/new_container --name new_container ruby bash
root@7a00af9d4d96:/#

We can easily copy a file between our docker container and the host directory. Also, note that files created in the host directory will automatically be available in the docker container directory mounted to the host.

Use COPY to Copy Files From Docker Container to Host

When creating a docker image using a docker file, we can also copy files between the docker host and the container using the COPY command.

Syntax:

COPY <SRC> <DEST>

In the DockerFile below, we specified ./app.py as the source directory and /var/www/app.py as the target directory.

#  base image
FROM python

# Set your working directory
WORKDIR /var/www/

# Copy the necessary filesls
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt

# Install the necessary packages
RUN pip install -r /var/www/requirements.txt

# Run the app
ENTRYPOINT ["echo", "Hello, Developer"]

This DockerFile creates an image of a simple Flask application that prints a message on the console.

Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Container