How to copy files from host to Docker container?

DockerDocker Container

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

The cp command can be used to copy files.

One specific file can be copied TO the container like:

docker cp foo.txt container_id:/foo.txt

One specific file can be copied FROM the container like:

docker cp container_id:/foo.txt foo.txt

For emphasis, container_id is a container ID, not an image ID. (Use docker ps to view listing which includes container_ids.)

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

docker cp src/. container_id:/target
docker cp container_id:/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.

Solution 2 - Docker

  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
    

###EXAMPLE:

$ docker ps

CONTAINER ID      IMAGE    COMMAND       CREATED      STATUS       PORTS        NAMES

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

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

or

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

d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5

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

Solution 3 - Docker

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

Solution 4 - Docker

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

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

Solution 5 - Docker

Typically there are three types:

  1. From a container to the host

    docker cp container_id:./bar/foo.txt .
    

dev1

  1. From the host to a container

    docker exec -i container_id sh -c 'cat > ./bar/foo.txt' < ./foo.txt
    
  2. Second approach to copy from host to container:

    docker cp foo.txt mycontainer:/foo.txt
    

dev2

  1. From a container to a container mixes 1 and 2

    docker cp container_id1:./bar/foo.txt .
    
    docker exec -i container_id2 sh -c 'cat > ./bar/foo.txt' < ./foo.txt
    

dev3

Solution 6 - Docker

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
root@b9b7400ddd8f:/# ls /var/www
file1  file2

Solution 7 - Docker

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

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

     docker build .
    

Solution 8 - Docker

The solution is given below,

###From the Docker shell,

root@123abc:/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.

Solution 9 - Docker

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

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

Copies the file foo.sh into /tmp of the container.

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

Solution 10 - Docker

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.

Solution 11 - Docker

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?.

Solution 12 - Docker

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?

 docker ps 

There is a NAMES section. Don't use aIMAGE.

Solution 13 - Docker

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.

Solution 14 - Docker

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

docker cp {SOURCE_FILE} {DESTINATION_CONTAINER_ID}:/{DESTINATION_PATH}

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 docker.com.

Solution 15 - Docker

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.

Solution 16 - Docker

This is the command to copy data from Docker to Host:

docker cp container_id:file path/filename /hostpath

docker cp a13fb9c9e674:/tmp/dgController.log /tmp/

Below is the command to copy data from host to docker:

docker cp a.txt ccfbeb35116b:/home/

Solution 17 - Docker

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

/var/lib/docker/aufs/required-docker-id/

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/

Solution 18 - Docker

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"

Solution 19 - Docker

Docker cp command is a handy utility that allows to copy files and folders between a container and the host system.

If you want to copy files from your host system to the container, you should use docker cp command like this:

docker cp host_source_path container:destination_path

List your running containers first using docker ps command:

abhishek@linuxhandbook:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
  PORTS               NAMES
  8353c6f43fba        775349758637        "bash"              8 seconds ago       Up 7 
  seconds                            ubu_container

You need to know either the container ID or the container name. In my case, the docker container name is ubu_container. and the container ID is 8353c6f43fba.

If you want to verify that the files have been copied successfully, you can enter your container in the following manner and then use regular Linux commands:

docker exec -it ubu_container bash

Copy files from host system to docker container Copying with docker cp is similar to the copy command in Linux.

I am going to copy a file named a.py to the home/dir1 directory in the container.

docker cp a.py ubu_container:/home/dir1

If the file is successfully copied, you won’t see any output on the screen. If the destination path doesn’t exist, you would see an error:

abhishek@linuxhandbook:~$ sudo docker cp a.txt ubu_container:/home/dir2/subsub
        Error: No such container:path: ubu_container:/home/dir2

If the destination file already exists, it will be overwritten without any warning.

You may also use container ID instead of the container name:

docker cp a.py 8353c6f43fba:/home/dir1

Solution 20 - Docker

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".

Solution 21 - Docker

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

Solution 22 - Docker

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

Solution 23 - Docker

docker cp SRC_PATH CONTAINER_ID:DEST_PATH

For example, I want to copy my file xxxx/download/jenkins to tomcat

I start to get the id of the container Tomcat

docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
63686740b488        tomcat              "catalina.sh run"   12 seconds ago      Up 11 seconds       0.0.0.0:8080->8080/tcp   peaceful_babbage

docker cp xxxx/download/jenkins.war  63686740b488:usr/local/tomcat/webapps/

Solution 24 - Docker

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

The destination path must be pre-exist

Solution 25 - Docker

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.

Solution 26 - Docker

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.

Solution 27 - Docker

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

Solution 28 - Docker

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

/var/lib/docker/devicemapper/mnt/<id>/rootfs/

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.

Solution 29 - Docker

My favorite method:

CONTAINERS:

CONTAINER_ID=$(docker ps | grep <string> | awk '{ print $1 }' | xargs docker inspect -f '{{.Id}}')
file.txt
mv -f file.txt /var/lib/docker/devicemapper/mnt/$CONTAINER_ID/rootfs/root/file.txt

or

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

Solution 30 - Docker

I just started using docker to compile VLC, here's what you can do to copy files back and forth from containers:

su -
cd /var/lib/docker
ls -palR > /home/user/dockerfilelist.txt

Search for a familiar file in that txt and you'll have the folder, cd to it as root and voila! copy all you want.

There might be a path with "merged" in it, I guess you want the one with "diff" in it.

Also if you exit the container and want to be back where you left off:

docker ps -a
docker start -i containerid

I guess that's usefull when you didn't name anything with a command like

docker run -it registry.videolan.org:5000/vlc-debian-win64 /bin/bash

Sure the hacker method but so what!

Solution 31 - Docker

This is what worked for me

#Run the docker image in detached mode 
$docker run -it -d test:latest bash

$ docker ps
CONTAINER ID        IMAGE               COMMAND
a6c1ff6348f4        test:latest     "bash"

#Copy file from host to container
sudo docker cp file.txt a6c1ff6348f4:/tmp

#Copy the file from container to host
docker cp test:/tmp/file.txt /home

Solution 32 - Docker

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

ifconfig

Then just enter into your Docker container and type

scp user_name@ip_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

Solution 33 - Docker

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.

Solution 34 - Docker

I wanted to have a build process happen in the container without files like the .git folder, but I want those files when I run the container interactively to debug problems. Like Ben Davis's answer, this executes the file copy within the container. But I don't want to have to actually run that command myself when I enter the container. Thus the following script will do the trick:

# mount the current [project] directory as read only
docker run --name my_container -v $(pwd):/mnt/application:ro -itd my_image /bin/bash
# copy the missing project files from the mounted directory
docker exec -it my_container /bin/bash -c 'cp -rnT /mnt/application $HOME/application'
# interactively use the container
docker attach my_container

This leaves behind a my_container container instance. To run the command again, you would have to write docker rm my_container. Or instead, if you wanted to get into that container instance again, you could execute docker start my_container && docker attach my_container.

Note the cp -n flag, which prevents overwriting files if they already exist in the destination. The :ro portion does not allow the host files to be changed. And you could change $(pwd) to a specific directory, like /home/user/dev/application.

Solution 35 - Docker

You can use below commands

  1. Copy file from host machine to docker container

    docker cp /hostfile container_id:/to_the_place_you_want_the_file_to_be

  2. Copy file from docker container to host machine

    docker cp container_id:src_path to_the_place_you_want_the_file_to_be

Solution 36 - Docker

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

For example:

First find out the container id:

root@**3aed62678d54**:/home#

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

root@saasdock:/home/dnepangue# cp cheering_nasa.gif /var/lib/docker/aufs/mnt/**3aed62678d54**a5df47a4a00a58bb0312009c2902f8a37498a1427052e8ac454b/home/

Back to the container...

root@**3aed62678d54**:/home# ls cheering_nasa.gif

Solution 37 - Docker

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.

Solution 38 - Docker

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"

then

docker exec -it mntcontainer bash

Solution 39 - Docker

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 FILE_PATH_ON_YOUR_HOST IP_CONTAINER:DESTINATION_PATH
scp foo.txt 172.17.0.2:foo.txt

In the case you need to copy a directory:

scp -r DIR_PATH_ON_YOUR_HOST IP_CONTAINER:DESTINATION_PATH
scp -r directory 172.17.0.2:directory

be sure to install ssh into your container too.

apt-get install openssh-server

Solution 40 - Docker

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.

Solution 41 - Docker

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!

Solution 42 - Docker

The simpliest way to achieve this is,

  1. Find the container id
  2. docker cp <filename> <container-id>:<path>

Solution 43 - Docker

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH to copy from host machine to container.

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH to copy from the container to the host machine.

Solution 44 - Docker

Try docker cp.

Usage:

docker cp CONTAINER:PATH HOSTPATH

It copies files/folders from PATH to the HOSTPATH.

Solution 45 - Docker

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.

Solution 46 - Docker

Copy from container to host dir

docker cp [container-name/id]:./app/[index.js] index.js

(assume you have created a workdir /app in your dockerfile)

Copy from host to container

docker cp index.js [container-name/id]:./app/index.js

Solution 47 - Docker

real deal is:

docker volume create xdata
docker volume inspect xdata

so you see its mount poit dir.
just copy your stuff there
then

docker run  --name=example1 --mount source=xdata,destination=/xdata -it yessa bash

where yessa is image name

Solution 48 - Docker

If you're able to use a container registry, the appendlayer Python package does what you're asking for:

Assume that ubuntu:base is already available in the registry.

You can then add a new layer consisting of some local files on top of it using the script, saving the whole thing as a new image (i.e. ubuntu:test).

$ pip install appendlayer
$ tar cvf - test.txt | appendlayer <host> <repository> <old-tag> <new-tag>

All without having to rebuild or even download any of the image data to your local machine.

Solution 49 - Docker

Using the following command from outside the container, I was able to copy file from my host machine to the container.

 487b00c94dd4 = Container ID
D:\demo\demo.tar= Source Url
/myfolder= Container Url

 
docker cp "D:\demo\demo.tar" 487b00c94dd4:/myfolder

Solution 50 - Docker

Bring dotfile into shell session (using base64 escape)

There are times when executing a separate interactive command to the shell launch is cumbersome. Here's what I do when I want to copy a dotfile into my container so my shell will contain my customizations

docker exec -u root -it mycontainername bash -c "echo "$(cat ~/.bashrc | base64 -w 0)" | base64 --decode > /tmp/.bashrc_inside_container; bash --rcfile /tmp/.bashrc_inside_container"

Solution 51 - Docker

I would highly recommend that you do not copy any files to any container, instead use a docker volume. This will allow you to persist files on your host. If you do not do it this way, then when its time to upgrade your docker image/container (you should do this on any release that contains any CVE's) then you would be removing the files you just uploaded.

I SFTP to my Docker Host (Linux Server usually), uploading the file there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3001829View Question on Stackoverflow
Solution 1 - DockerHenrik SachseView Answer on Stackoverflow
Solution 2 - DockersolidleonView Answer on Stackoverflow
Solution 3 - DockerBen DavisView Answer on Stackoverflow
Solution 4 - DockerErikView Answer on Stackoverflow
Solution 5 - DockerAli HallajiView Answer on Stackoverflow
Solution 6 - DockeradeanView Answer on Stackoverflow
Solution 7 - DockerRustyShacklefordView Answer on Stackoverflow
Solution 8 - DockerpierceView Answer on Stackoverflow
Solution 9 - DockerjoematView Answer on Stackoverflow
Solution 10 - DockerAndreaView Answer on Stackoverflow
Solution 11 - DockerHiro.ProtagonistView Answer on Stackoverflow
Solution 12 - DockeraerinView Answer on Stackoverflow
Solution 13 - DockerjoematView Answer on Stackoverflow
Solution 14 - DockerprayagupaView Answer on Stackoverflow
Solution 15 - DockerjohndodoView Answer on Stackoverflow
Solution 16 - DockerManinderView Answer on Stackoverflow
Solution 17 - Docker0x3bfcView Answer on Stackoverflow
Solution 18 - DockerIsura NimalasiriView Answer on Stackoverflow
Solution 19 - DockerAbhishek PatwaView Answer on Stackoverflow
Solution 20 - DockerAbdennour TOUMIView Answer on Stackoverflow
Solution 21 - DockerEverett ToewsView Answer on Stackoverflow
Solution 22 - Dockerishandutta2007View Answer on Stackoverflow
Solution 23 - DockerMourad MAMASSIView Answer on Stackoverflow
Solution 24 - DockerAditya BhuyanView Answer on Stackoverflow
Solution 25 - DockerKlas MellbournView Answer on Stackoverflow
Solution 26 - Dockerkarthik101View Answer on Stackoverflow
Solution 27 - DockerPrakash VarandaniView Answer on Stackoverflow
Solution 28 - DockerakostadinovView Answer on Stackoverflow
Solution 29 - Dockeruser3159411View Answer on Stackoverflow
Solution 30 - Dockercolin lamarreView Answer on Stackoverflow
Solution 31 - DockerJafar KaruthedathView Answer on Stackoverflow
Solution 32 - DockerHarshit AnandView Answer on Stackoverflow
Solution 33 - Dockerh.aittamaaView Answer on Stackoverflow
Solution 34 - DockerErasmusView Answer on Stackoverflow
Solution 35 - DockerPankaj AgrawalView Answer on Stackoverflow
Solution 36 - DockerasterinuxView Answer on Stackoverflow
Solution 37 - Dockeruser3392439View Answer on Stackoverflow
Solution 38 - DockerEast2WestView Answer on Stackoverflow
Solution 39 - DockergaetanoView Answer on Stackoverflow
Solution 40 - DockerR.KView Answer on Stackoverflow
Solution 41 - DockerHarshal VaidyaView Answer on Stackoverflow
Solution 42 - DockerSoumyajitView Answer on Stackoverflow
Solution 43 - DockerChiranjeevi KandelView Answer on Stackoverflow
Solution 44 - DockergangaView Answer on Stackoverflow
Solution 45 - DockerterminaliView Answer on Stackoverflow
Solution 46 - DockerNiyas AliView Answer on Stackoverflow
Solution 47 - DockerАлексей НеудачинView Answer on Stackoverflow
Solution 48 - DockermaltheView Answer on Stackoverflow
Solution 49 - DockerSayed Uz ZamanView Answer on Stackoverflow
Solution 50 - DockerSridhar SarnobatView Answer on Stackoverflow
Solution 51 - DockerWilliam DaughertyView Answer on Stackoverflow