How to copy files from host to Docker container?

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

    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.

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

    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

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

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

    docker run -i ubuntu /bin/bash -c 'cat > file' < file
    
    0 讨论(0)
  • 2020-11-22 07:38

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

    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.

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

    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.

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