How to copy files from host to Docker container?

前端 未结 30 2474
南方客
南方客 2020-11-22 06:58

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

相关标签:
30条回答
  • 2020-11-22 07:14

    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/
    
    0 讨论(0)
  • 2020-11-22 07:15

    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.

    0 讨论(0)
  • 2020-11-22 07:17

    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.

    0 讨论(0)
  • 2020-11-22 07:18

    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/

    0 讨论(0)
  • 2020-11-22 07:19

    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
    b9b7400ffffd8f        ubuntu:latest                "/bin/bash"         2 seconds ago       Up 2 seconds                                elated_hodgkin
    

    In the example above we can either use b9b7400ffffd8f 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@b9b7400ffffd8f:/# ls /var/www
    file1  file2
    
    0 讨论(0)
  • 2020-11-22 07: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.

    0 讨论(0)
提交回复
热议问题