本文翻译自:Copying files from host to Docker container
I am trying to build a backup and restore solution for the Docker containers that we work with. 我正在尝试为我们使用的Docker容器构建备份和还原解决方案。
I have Docker base image that I have created, ubuntu:base
, and do not want have to rebuild it each time with a Docker file to add files to it. 我有我创建的Docker基本映像ubuntu:base
,并且不想每次都必须使用Docker文件重建它来向其添加文件。
I want to create a script that runs from the host machine and creates a new container using the ubuntu:base
Docker image and then copies files into that container. 我想创建一个从主机运行的脚本,并使用ubuntu:base
Docker映像创建一个新容器,然后将文件复制到该容器中。
How can I copy files from the host to the container? 如何将文件从主机复制到容器?
#1楼
参考:https://stackoom.com/question/1Y7DT/将文件从主机复制到Docker容器
#2楼
以下是执行此操作的相当难看的方法,但它可以工作。
docker run -i ubuntu /bin/bash -c 'cat > file' < file
#3楼
The solution is given below, 解决方案如下:
From the Docker shell, 在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. 该文件应直接复制到容器在文件系统上的位置。
#4楼
I simply copy the file directly from where the container is located from the host machine. 我只是直接从主机从容器所在的位置复制文件。
For example: 例如:
First find out the container id: 首先找出容器编号:
root@**3aed62678d54**:/home#
And then from the host, let's say the file is in the home directory: 然后从主机,假设文件位于主目录中:
root@saasdock:/home/dnepangue# cp cheering_nasa.gif /var/lib/docker/aufs/mnt/**3aed62678d54**a5df47a4a00a58bb0312009c2902f8a37498a1427052e8ac454b/home/
Back to the container... 回到容器...
root@**3aed62678d54**:/home# ls cheering_nasa.gif
#5楼
Get container name or short container id: 获取容器名称或简短的容器ID:
$ docker ps
Get full container id: 获取完整的容器ID:
$ docker inspect -f '{{.Id}}' SHORT_CONTAINER_ID-or-CONTAINER_NAME
Copy file: 复制文件:
$ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE
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 要么
$ docker inspect -f '{{.Id}}' d8e703d7e303
d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5
$ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5**/root/file.txt
#6楼
将文件复制到我发现的容器的最好方法是使用docker run命令的-v
选项在主机上安装目录。