Copying files from host to Docker container

user3001829 Source

I am trying to build a backup and restore solution for the Docker containers that we work with.

I have Docker base image that I have created, ubuntu:base, and do not want have to rebuild it each time with a Docker file to add files to it.

I want to create a script that runs from the host machine and creates a new container using the ubuntu:base Docker image and then copies files into that container.

How can I copy files from the host to the container?



answered 4 years ago Erik #1

The following is a fairly ugly way of doing it but it works.

docker run -i ubuntu /bin/bash -c 'cat > file' < file

answered 4 years ago pierce #2

The solution is given below,

From the Docker shell,

[email protected]:/root#  <-- get the container ID

From the host

cp thefile.txt /var/lib/docker/devicemapper/mnt/123abc<bunch-o-hex>/rootfs/root

The file shall be directly copied to the location where the container sits on the filesystem.

answered 4 years ago asterinux #3

I simply copy the file directly from where the container is located from the host machine.

For example:

First find out the container id:

[email protected]**3aed62678d54**:/home#

And then from the host, let's say the file is in the home directory:

[email protected]:/home/dnepangue# cp cheering_nasa.gif /var/lib/docker/aufs/mnt/**3aed62678d54**a5df47a4a00a58bb0312009c2902f8a37498a1427052e8ac454b/home/

Back to the container...

[email protected]**3aed62678d54**:/home# ls cheering_nasa.gif

answered 4 years ago solidleon #4

  1. Get container name or short container id:

    $ docker ps
  2. Get full container id:

    $ docker inspect -f   '{{.Id}}'  SHORT_CONTAINER_ID-or-CONTAINER_NAME
  3. Copy file:

    $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE


$ docker ps


d8e703d7e303   solidleon/ssh:latest      /usr/sbin/sshd -D                      cranky_pare

$ docker inspect -f   '{{.Id}}' cranky_pare


$ docker inspect -f   '{{.Id}}' d8e703d7e303


$ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5**/root/file.txt

answered 4 years ago Prakash Varandani #5

The best way for copying files to the container I found is mounting a directory on host using -v option of docker run command.

answered 4 years ago Ben Davis #6

The cleanest way is to mount a host directory on the container when starting the container:

{host} docker run -v /path/to/hostdir:/mnt --name my_container my_image
{host} docker exec -it my_container bash
{container} cp /mnt/sourcefile /path/to/destfile

answered 4 years ago user3392439 #7

Where you don't have a directory defined as a volume in the Dockerfile, the /var/lib/docker/aufs/mnt// will work. But there are cases where the directory within the container is defined as a volume. In this case, the contents under aufs/mnt/*/ and the contents seen by the container are different.

You will need to inspect the container using docker inspect and then, look for volumes. There you will find a mention for something like /var/lib/docker/vfs/dir/fe940b... (the id). You will need to add/modify the files here instead of under aufs/mnt/*.

The confusing part is that the files also appear under /aufs/mnt/*. I spent quite a while scratching my head why changes here didn't work for me. Hope this helps someone.

answered 4 years ago johndodo #8

Assuming the container is already running, type the given command:

# cat /path/to/host/file/ | docker exec -i -t <container_id> bash -c "/bin/cat > /path/to/container/file"

To share files using shared directory, run the container by typing the given command:

# docker run -v /path/to/host/dir:/path/to/container/dir ...

Note: Problems with permissions might arise as container's users are not the same as the host's users.

answered 4 years ago akostadinov #9

There are good answers, but too specific. I find out docker ps is good way to get container id you're interested in. Then do

mount | grep <id>

to see where the volume is mounted. That's


for me, but it might be a different path depending on the OS and configuration. Now simply copy files to that path.

Using -v is not always practical.

answered 4 years ago ganga #10

Try docker cp.



It copies files/folders from PATH to the HOSTPATH.

answered 4 years ago user3159411 #11

My favorite method:


CONTAINER_ID=$(docker ps | grep <string> | awk '{ print $1 }' | xargs docker inspect -f '{{.Id}}')


mv -f file.txt /var/lib/docker/devicemapper/mnt/$CONTAINER_ID/rootfs/root/file.txt


mv -f file.txt /var/lib/docker/aufs/mnt/$CONTAINER_ID/rootfs/root/file.txt

answered 4 years ago adean #12

If you need to do this on a running container you can use docker exec (added in 1.3).

First, find the container's name or ID:

$ docker ps
CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS              PORTS                   NAMES
b9b7400ddd8f        ubuntu:latest                "/bin/bash"         2 seconds ago       Up 2 seconds                                elated_hodgkin

In the example above we can either use b9b7400ddd8f or elated_hodgkin.

If you wanted to copy everything in /tmp/somefiles on the host to /var/www in the container:

$ cd /tmp/somefiles
$ tar -cv * | docker exec -i elated_hodgkin tar x -C /var/www

We can then exec /bin/bash in the container and verify it worked:

$ docker exec -it elated_hodgkin /bin/bash
[email protected]:/# ls /var/www
file1  file2

answered 3 years ago joemat #13

Another solution for copying files into a running container is using tar:

tar -c | docker exec -i theDockerContainer /bin/tar -C /tmp -x

Copies the file into /tmp of the container.

Edit: Remove reduntant -f, thanks to Maartens comment.

answered 3 years ago aabdulwahed #14

In a docker environment, all containers are found in the directory:


To copy the source directory/file to any part of the container, type the given command:

sudo cp -r mydir/ /var/lib/docker/aufs/mnt/required-docker-id/mnt/

answered 3 years ago Klas Mellbourn #15

Many that find this question may actually have the problem of copying files into a Docker image while it is being created (I did).

In that case, you can use the COPY command in the Dockerfile that you use to create the image.

See the documentation.

answered 3 years ago Andrea #16

To copy a file from host to running container

docker exec -i $CONTAINER /bin/bash -c "cat > $CONTAINER_PATH" < $HOST_PATH

Based on Erik's answer and Mikl's and z0r's comments.

answered 3 years ago Rusty Shackleford #17

Very easy

  1. Create a new dockerfile and use the existing image as your base.

    FROM myName/myImage:latest
    ADD bin/
  2. Then build the container:

    docker build .

answered 3 years ago Harshit Anand #18

You can just trace the IP address of your local machine using


Then just enter into your Docker container and type

scp [email protected]_address:/path_to_the_file destination

In any case if you don't have an SSH client and server installed, just install it using:

sudo apt-get install openssh-server

answered 3 years ago joemat #19

With Docker 1.8, docker cp is able to copy files from host to container. See the Docker blog post Announcing Docker 1.8: Content Trust, Toolbox, and Updates to Registry and Orchestration.

answered 3 years ago h3nrik #20

The cp command can be used to copy files. One specific file can be copied like:

docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt

For emphasis, mycontainer is a container ID, not an image ID.

Multiple files contained by the folder src can be copied into the target folder using:

docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

Reference: Docker CLI docs for cp

In Docker versions prior to 1.8 it was only possible to copy files from a container to the host. Not from the host to a container.

answered 3 years ago Hiro.Protagonist #21

This is a direct answer to the question 'Copying files from host to Docker container' raised in this question in the title.

Try docker cp. It is the easiest way to do that and works even on my Mac. Usage:

docker cp /root/some-file.txt some-docker-container:/root

This will copy the file some-file.txt in the directory /root on your host machine into the Docker container named some-docker-container into the directory /root. It is very close to the secure copy syntax. And as shown in the previous post, you can use it vice versa. I.e., you also copy files from the container to the host.

And before you downlink this post, please enter docker cp --help. Reading the documentation can be very helpful, sometimes...

If you don't like that way and you want data volumes in your already created and running container, then recreation is your only option today. See also How can I add a volume to an existing Docker container?.

answered 3 years ago East2West #22

I'd mount and then run the image with a daemon, just any as given here;

docker run -d -v /blah1/blah2:/mnt --name mntcontainer ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"


docker exec -it mntcontainer bash

answered 3 years ago Abdennour TOUMI #23

If the host is CentOS or Fedora, there is a proxy NOT in /var/lib/docker/aufs, but it is under /proc:

cp -r /home/user/mydata/* /proc/$(docker inspect --format "{{.State.Pid}}" <containerid>)/root

This cmd will copy all contents of data directory to / of container with id "containerid".

answered 2 years ago Everett Toews #24

tar and docker cp are a good combo for copying everything in a directory.

Create a data volume container

docker create --name dvc --volume /path/on/container cirros

To preserve the directory hierarchy

tar -c -C /path/on/local/machine . | docker cp - dvc:/path/on/container

Check your work

docker run --rm --volumes-from dvc cirros ls -al /path/on/container

answered 2 years ago gaetano #25

Another workaround is using the good old scp. This is useful in the case you need to copy a directory.

From your host run:

scp foo.txt

In the case you need to copy a directory:

scp -r directory

be sure to install ssh into your container too.

apt-get install openssh-server

answered 2 years ago prayagupd #26

To copy files/folders between a container and the local filesystem, type the command:


For example,

docker cp /home/foo container-id:/home/dir

To get the contianer id, type the given command:

docker ps

The above content is taken from

answered 2 years ago R.K #27

If using Windows as host, you can use WinSCP to connect to Docker and transfer files through the GUI.

If on Linux, the scp command would also work through the terminal.

answered 1 year ago Harshal Vaidya #28

One thing which I tried and it worked

Once you spin up your docker container and if you create any file under that container; You can easily access that file from below location of your docker host:-

cd /var/lib/docker/aufs/containers/container_id/tmp

Try once!

answered 11 months ago karthik101 #29

This is a onliner for copying a single file while running a tomcat container.

docker run -v /PATH_TO_WAR/sample.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat

This will copy the war file to webapps directory and get your app running in no time.

answered 9 months ago terminali #30

I usually create python server using this command

python -m SimpleHTTPServer

in the particular directory and then just use wget to transfer file in the desired location in docker. I know it is not the best way to do it but I find it much easier.

answered 8 months ago Aaron #31

I tried most of the (upvoted) solutions here but in docker 17.09 (in 2018) there is no longer /var/lib/docker/aufs folder.

This simple docker cp solved this task.

docker cp c:\path\to\local\file container_name:/path/to/target/dir/

How to get container_name? (There is NAMES section. Don't use IMAGE.)

 docker ps 

answered 4 months ago h.aittamaa #32

In my opinion you have not to copy files inside image but you can use GIT or SVN for your files and then set a volume synchronized with a local folder. Use a script while runing container who can check if data already exist in local folder if not copy it from GIT repository. That make your image very lightweight.

answered 2 months ago Isura Nimalasiri #33

Container Up Syntax:

 docker run -v /HOST/folder:/Container/floder 

In docker File

COPY hom* /myFolder/        # adds all files starting with "hom"
COPY hom?.txt /myFolder/    # ? is replaced with any single character, e.g., "home.txt"

answered 2 months ago ishandutta2007 #34

In case it is not clear to someone like me what mycontainer in @h3nrik answer means, it is actually the container id. To copy a file WarpSquare.mp4 in /app/example_scenes/1440p60 from an exited docker container to current folder I used this.

docker cp `docker ps -q -l`:/app/example_scenes/1440p60/WarpSquare.mp4 .

where docker ps -q -l pulls up the container id of the last exited instance. In case it is not an exited container you can get it by docker container ls or docker ps

comments powered by Disqus