docker tutorials

How to Copy Files between Host and Docker Container ?

This tutorial guides you on how to copy files between host and docker container. Let’s see how to copy files between running docker container and host file system using docker cp command and verify the results.

Copy files between host and docker container

The following docker cp commands can be used to copy files between host and docker container.

docker cp <source path> <container>:<destination path>

docker cp <container>:<source path> <destination path>

The docker cp command utility copies the contents of <source path> to the <destination path>. You can copy the files from local host machine to the docker container and vice versa.

Note, the docker cp command assumes that docker container paths are relative to it’s root directory “/”. It means that the following commands are same irrespective whether you use slash or not in the beginning of the path and it will work.

<container>:/tmp/readme.txt
(OR)
<container>:tmp/readme.txt

Now, let’s get in to action. First, let’s see how to copy files from host to docker container and verify the same using docker exec command.

Copy files from host to docker container

I am going to use the following Dockerfile that I have used in the tutorial Docker ADD Vs COPY Instruction for our exercise. Place this Dockerfile in your project directory.

FROM ubuntu //STEP 1

MAINTAINER Sneppets Admin <[email protected]> //STEP 2

RUN apt-get update && apt-get install -y openssh-server //STEP 3

RUN mkdir -p /var/run/sshd //STEP 4

#ADD sshd_config /etc/ssh/sshd_config

COPY sshd_config /etc/ssh/sshd_config //STEP 5

ADD sample_tar.tar tar //STEP 6

ADD https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf /pdf/dummy.pdf //STEP 7

CMD /usr/sbin/sshd -D //STEP 8

Then run the following docker build command to build docker image.

$ docker build -t sneppets/ubuntu_sshd .

Check whether docker image is created.

$ docker images

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
sneppets/ubuntu_sshd   latest              cf0472974329        14 minutes ago      222MB
ubuntu                 latest              1e4467b07108        6 days ago          73.9MB

Run the docker container and check whether container is started, up and running with the following commands.

//Run the docker container
$ docker run sneppets/ubuntu_sshd

//check the container status
$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS               NAMES
e62c8dc12c14        sneppets/ubuntu_sshd   "/bin/sh -c '/usr/sb…"   6 seconds ago       Up 4 seconds                            sharp_mclaren

Let’s say the file “names.zip” need to be copied from host machine to docker container.

$ ls
prototype  sshd-example  gs-spring-boot  names.zip

The following is the docker command that can be used to copy files from host to docker container.

$ docker cp names.zip e62c8dc12c14:/names.zip

To verify the files copied to docker container, try to run the following docker exec command. This command helps in running a command in a running container.

$ docker exec -it e62c8dc12c14 /bin/bash
root@e62c8dc12c14:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  names.zip  opt  pdf  proc  root  run  sbin  srv  sys  tar  tmp  usr  var
root@e62c8dc12c14:/#

You should be able to see the file “names.zip” is copied to the running docker container from host machine.

Copy files from docker container to host

Let’s create sample “read.txt” file in the docker container file system as shown.

# touch read.txt
root@e62c8dc12c14:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  names.zip  opt  pdf  proc  read.txt  root  run  sbin  srv  sys  tar  tmp  usr  var
root@e62c8dc12c14:/# exit
exit

Now, use the following command to copy files from running docker container to the host file system.

$ docker cp e62c8dc12c14:/read.txt read.txt

Finally, the file is copied in to the host file system.

$ ls
prototype  sshd-example  gs-spring-boot  names.zip read.txt

That’s it. Hope it helped 🙂

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments