Docker:将文件从 Docker 容器复制到主机

Docker:将文件从 Docker 容器复制到主机

问题描述:

我正在考虑使用 Docker 在持续集成 (CI) 服务器上构建我的依赖项,这样我就不必在代理本身上安装所有运行时和库.

I'm thinking of using Docker to build my dependencies on a Continuous Integration (CI) server, so that I don't have to install all the runtimes and libraries on the agents themselves.

为了实现这一点,我需要将在容器内构建的构建工件复制回主机.这可能吗?

To achieve this I would need to copy the build artifacts that are built inside the container back into the host. Is that possible?

为了将文件从容器复制到主机,可以使用命令

In order to copy a file from a container to the host, you can use the command

docker cp <containerId>:/file/path/within/container /host/path/target

这是一个例子:

$ sudo docker cp goofy_roentgen:/out_read.jpg .

这里 goofy_roentgen 是我从以下命令获得的容器名称:

Here goofy_roentgen is the container name I got from the following command:

$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
1b4ad9311e93        bamos/openface      "/bin/bash"         33 minutes ago      Up 33 minutes       0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp   goofy_roentgen

您也可以使用(部分)容器 ID.下面的命令等价于第一个

You can also use (part of) the Container ID. The following command is equivalent to the first

$ sudo docker cp 1b4a:/out_read.jpg .