Categories
docker docker-container

How to copy files from host to Docker container?

2188

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?

4

  • 15

    If you don’t want to rebuild, why not “docker commit” ? That saves your image.

    Apr 28, 2014 at 22:24

  • 59

    Just a remark on a notion nobody has addressed: in general, treat containers as “ephemeral”. There ARE use cases to copy files into/from a running container (testing, prototyping). But if you find yourself in a position where you can’t rebuild what you need using Dockerfiles and/or compose, then you may be in a bad place. You generally don’t want to be backing up containers as if they were OS or even VM objects. Generally speaking 🙂

    Oct 16, 2017 at 18:04


  • Possible duplicate of Copying files from Docker container to host

    – Shubham

    Nov 30, 2018 at 5:49

  • @BerenddeBoer’s link is now outdated, here’s the new URL: docs.docker.com/engine/reference/commandline/commit

    – Aaron

    Aug 21, 2020 at 2:45

3650

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.

13

  • 2

    also note this can be on the host vm or main OS and works either host to container or vm to host (or main os)

    Nov 16, 2015 at 6:06

  • 13

    In a Dockerfile you can use the ADD keyword to add files during build time.

    Jun 30, 2016 at 17:49

  • 10

    @h3nrik COPY preferred over ADD when applicable.

    Aug 2, 2016 at 17:06


  • 23

    use docker cp to copy from container to host works well, but when use it to copy files from host to container, no effect… anybody know why ? docker version: Docker version 1.10.3, build cb079f6-unsupported

    – Ace.Yin

    Nov 29, 2016 at 5:28


  • 6

    Can i copy multiple files using docker cp from host to container?

    – Yogesh D

    Dec 1, 2016 at 21:57

214

  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

8

  • 13

    For me the host’s mounting path didn’t contain aufs but a devicemapper. Easiest way to check the containers mounting path (while it is running) is to run the command mount.

    – derenio

    Oct 3, 2014 at 7:54

  • 3

    I tried the above solution. It copied the files into the docker specific directory. However, when I use bash for docker container, the files dont show up there. Is there something I am missing ?

    – AppleBud

    Dec 30, 2014 at 12:30


  • 2

    The new path is /var/lib/docker/devicemapper/mnt/<<id>>/rootfs/

    – mcuadros

    Jan 14, 2015 at 10:28

  • 4

    For me, on Docker 1.4.1 (current latest), it’s /var/lib/docker/aufs/diff/<id>/

    Jan 22, 2015 at 8:24


  • 8

    what about for poor MacOS users? The mount directory is in /var/lib for me. find / -name docker was not helpful either.

    – prayagupa

    Dec 1, 2016 at 18:47

165

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

5

  • 3

    how can you run container? i thought you can do that only with an image.

    Feb 17, 2015 at 14:46

  • I can’t get it work when container is existing aka has run before. Tar below works nice.

    Apr 23, 2015 at 6:34

  • 1

    The only way this worked for me was attaching the volume alongside running the image (instantiating the container). Ended up with: docker run -d -p -v /host/path:/mnt --name container_name image_name

    – peter n

    Jun 13, 2015 at 7:02


  • 2

    This doesnt work for me. Atleast cannot run a container. Can run only image

    Jun 16, 2015 at 6:34

  • 2

    I’m amazed that docker cp is a one-way operation!

    Aug 27, 2015 at 17:40