如何将文件从主机复制到 Docker 容器? [英] How to copy files from host to Docker container?
问题描述
我正在尝试为我们使用的 Docker 容器构建备份和恢复解决方案.
I am trying to build a backup and restore solution for the Docker containers that we work with.
我有我创建的 Docker 基础镜像,ubuntu:base
,并且不想每次都使用 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.
我想创建一个从主机运行的脚本,并使用 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.
如何将文件从主机复制到容器?
How can I copy files from the host to the container?
推荐答案
cp
命令可用于复制文件.
The cp
command can be used to copy files.
可以将一个特定文件复制到容器中,例如:
One specific file can be copied TO the container like:
docker cp foo.txt container_id:/foo.txt
可以从容器中复制一个特定文件,例如:
One specific file can be copied FROM the container like:
docker cp container_id:/foo.txt foo.txt
强调一下,container_id
是一个容器 ID,不是图像 ID.(使用 docker ps
查看包含 container_id
s 的列表.)
For emphasis, container_id
is a container ID, not an image ID. (Use docker ps
to view listing which includes container_id
s.)
文件夹 src
中包含的多个文件可以使用以下命令复制到 target
文件夹中:
Multiple files contained by the folder src
can be copied into the target
folder using:
docker cp src/. container_id:/target
docker cp container_id:/src/. target
Reference: Docker CLI docs for cp
在 1.8 之前的 Docker 版本中,只能将文件从容器复制到主机.不是从主机到容器.
In Docker versions prior to 1.8 it was only possible to copy files from a container to the host. Not from the host to a container.
这篇关于如何将文件从主机复制到 Docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!