How to copy files from host to Docker container?

前端 未结 30 2535
南方客
南方客 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: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
    

提交回复
热议问题