Copy the files from the host to the Docker containers

This translation from: Copying Files from Host to Container Docker

I am trying to build a backup and restore solution for the Docker containers that we work with. I'm trying to build a backup and restore solution for Docker containers we use.

Docker Base Image that have have the I the I have have the Created, ubuntu:base, want and do not have have to rebuild the each IT A Time with Docker File to the Add Files to IT. I have Docker base image I created ubuntu:base, and do not want to have to use every Docker file reconstruction to add it to the file.

The I want to the Create A Script that runs from at The Host Machine and Creates A new new Container a using at The ubuntu:baseDocker Image and the then copies Files INTO that Container. I want to create a from a script host is running, and use ubuntu:baseto create a new container Docker image and then copy the file to the container.

How can I copy files from the host to the container? How to copy files from the host to the container?


#1st Floor

Reference: https://stackoom.com/question/1Y7DT/ to copy files from the host to the Docker containers


#2nd Floor

The following is a very ugly way to do this, but it can work.

docker run -i ubuntu /bin/bash -c 'cat > file' < file

#3rd floor

The solution is given below, the solution is as follows:

From the Docker shell, 在Docker Shell中,

root@123abc:/root#  <-- get the container ID

From the host 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. This file should be copied directly to the location of the container on the file system.


#4th floor

I simply copy the file directly from where the container is located from the host machine. I just copy the files from the location where the container directly from the host.

For example: For example:

First find out the container id: first identify the container number:

root@**3aed62678d54**:/home#

And then from the host, let's say the file is in the home directory: then located in the main directory from the host, assume the file:

root@saasdock:/home/dnepangue# cp cheering_nasa.gif /var/lib/docker/aufs/mnt/**3aed62678d54**a5df47a4a00a58bb0312009c2902f8a37498a1427052e8ac454b/home/

Back to the container ... back into the container ...

root@**3aed62678d54**:/home# ls cheering_nasa.gif

#5th Floor

  1. Get container name or short container id: get the container name or a short container ID:

     $ docker ps 
  2. Get full container id: get the full container ID:

     $ docker inspect -f '{{.Id}}' SHORT_CONTAINER_ID-or-CONTAINER_NAME 
  3. Copy file: Copy the file:

     $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE 

EXAMPLE: For 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 either

$ docker inspect -f   '{{.Id}}' d8e703d7e303

d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5

$ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5**/root/file.txt

#6th floor

The best way to copy the file to the container I found is to use a docker run command -voption to install directory on the host.

Original articles published 0 · won praise 136 · views 830 000 +

Guess you like

Origin blog.csdn.net/xfxf996/article/details/105240362