There are some situation where you want to copy files to and from a docker container so that we will have data accessabile in host system or within the container.

This can be achieved through multiple ways as listed below.

  1. Through docker cp command
  2. Through docker volumes

We will learn these two methods with some examples and explanations.

How to use docker cp command

Syntax:

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

So ‘docker cp’ command is similar to scp command.

Example: For copying a file(in this case a chef-server deb file) from the container we can use below command

First list available docker containers in your host:

docker ps

Note: We can not retrieve files from a dead/exited docker container, your docker container need to be in running state.

Example output of “docker ps

SurendraAnneMacBook-Pro:docker-chef-server surendraanne$ docker ps

CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                          NAMES

08a50f4ba59d        docker_chef-server                      "/bin/bash"              4 seconds ago       Up 2 seconds        443/tcp, 2223/tcp              bold_bell

93ef537a4e79        docker_chef-workstation:latest          "/usr/sbin/sshd -D"      11 hours ago        Up 11 hours         2224/tcp                       chef_workstation

27c20adf1c39        docker_chef-node:latest                 "/usr/sbin/sshd -D"      11 hours ago        Up 11 hours         2222/tcp                       chef_node

02d9a3f04162        docker-chef-server_chef-server:latest   "/opt/opscode/embedd…"   11 hours ago        Up 11 hours         80/tcp, 0.0.0.0:443->443/tcp   chef_server

So my docker container is docker_chef-server and the file is chef-server* is located in /

Docker cp example to copy file from container to host machine.

SurendraAnneMacBook-Pro:docker-chef-server surendraanne$ docker cp 08a50f4ba59d:/chef-server-core_13.1.13-1_amd64.deb .

SurendraAnneMacBook-Pro:docker-chef-server surendraanne$ ls

CHANGELOG.md backup.sh docker-compose.yml knife.rb

Dockerfile chef-server-core_13.1.13-1_amd64.deb init.rb logrotate

README.md chef-server.rb install.sh

SurendraAnneMacBook-Pro:docker-chef-server surendraanne$ =

Copy files using docker volumes

Docker volumes is nothing but a share between host machine and docker contain for persistent data. This is heavily used to make data persistent across container life cycle. If you have docker volumes attached to a container we can just use cp command to copy files from docker containers.

See if your container have volumes attached, we can use below.

Step1: Get container ID from ps from where you want to get file. This id is useful for inspecting the docker container properties.

docker ps

Output:

SurendraAnneMacBook-Pro:~ surendraanne$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

da59c665589c        ubuntu              "/bin/bash"         44 seconds ago      Up 43 seconds                           dazzling_wilbur

44ffd285a7a2        e3d0057c0316        "/bin/bash"         5 months ago        Up 2 minutes        80/tcp, 443/tcp     infallible_cohen

Now get the mount point details from the container ID.

SurendraAnneMacBook-Pro:~ surendraanne$ docker inspect -f "{{ .Mounts }}" da59c665589c

[{bind  /Users/surendraanne/mymount /root/test   true rprivate}]

From the above output I can say my local folder which is mounted on docker is /Users/surendraanne/mymount.

To copy a file from docker container location /root/test, we have to just copy from my local directory/Users/surendraanne/mymount on host machine.

Output:

SurendraAnneMacBook-Pro:mymount surendraanne$ pwd

/Users/surendraanne/mymount

SurendraAnneMacBook-Pro:mymount surendraanne$ ls -l

total 0

-rw-r--r--  1 surendraanne  staff  0 30 Jun 22:06 abc

-rw-r--r--  1 surendraanne  staff  0 30 Jun 22:07 xyz

SurendraAnneMacBook-Pro:mymount surendraanne$ 

Please share if you know any other methods to copy files to and from docker container

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.