How to copy files from host to Docker container?

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

    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.

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

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

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

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

    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!

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

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

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