Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

docker - 将文件从主机复制到Docker容器(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?

(如何将文件从主机复制到容器?)

  ask by user3001829 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The cp command can be used to copy files.

(cp命令可用于复制文件。)

One specific file can be copied TO the container like:

(可以将一个特定文件复制到容器中,例如:)

docker cp foo.txt mycontainer:/foo.txt

One specific file can be copied FROM the container like:

(可以从容器中复制一个特定文件,例如:)

docker cp mycontainer:/foo.txt foo.txt

For emphasis, mycontainer is a container ID, not an image ID.

(为了强调, mycontainer容器 ID, 而不是 图像 ID。)

Multiple files contained by the folder src can be copied into the target folder using:

(可以使用以下命令将文件夹src包含的多个文件复制到target文件夹中:)

docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

Reference: Docker CLI docs for cp

(参考: 适用于cp Docker CLI文档)

In Docker versions prior to 1.8 it was only possible to copy files from a container to the host.

(在1.8之前的Docker版本中,只能将文件从容器复制到主机。)

Not from the host to a container.

(不是从主机到容器。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...