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

本文翻译自:Copying files from Docker container to host

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

To achieve this I would need to copy the build artifacts that are built inside the container back into the host. 为此,我需要将容器内部构建的构建工件复制回主机。 Is that possible? 那可能吗?


#1楼

参考:https://stackoom.com/question/1UW0S/将文件从Docker容器复制到主机


#2楼

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

Here's an example: 这是一个例子:

$ sudo docker cp goofy_roentgen:/out_read.jpg .

Here goofy_roentgen is the container name I got from the following command: 这里goofy_roentgen是我从以下命令获得的容器名称:

$ 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

You can also use (part of) the Container ID . 您还可以使用(部分) 容器ID The following command is equivalent to the first 以下命令等效于第一个

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

#3楼

Mount a "volume" and copy the artifacts into there: 挂载“卷”,然后将工件复制到其中:

mkdir artifacts
docker run -i -v ${PWD}/artifacts:/artifacts ubuntu:14.04 sh << COMMANDS
# ... build software here ...
cp <artifact> /artifacts
# ... copy more artifacts into `/artifacts` ...
COMMANDS

Then when the build finishes and the container is no longer running, it has already copied the artifacts from the build into the artifacts directory on the host. 然后,当构建完成并且容器不再运行时,它已将构件从构建复制到主机上的artifacts目录中。

Edit 编辑

Caveat: When you do this, you may run into problems with the user id of the docker user matching the user id of the current running user. 注意:执行此操作时,可能会遇到docker用户的用户ID与当前运行用户的用户ID匹配的问题。 That is, the files in /artifacts will be shown as owned by the user with the UID of the user used inside the docker container. 也就是说, /artifacts的文件将显示为用户拥有,并在docker容器内使用用户的UID。 A way around this may be to use the calling user's UID: 一种解决方法是使用主叫用户的UID:

docker run -i -v ${PWD}:/working_dir -w /working_dir -u $(id -u) \
    ubuntu:14.04 sh << COMMANDS
# Since $(id -u) owns /working_dir, you should be okay running commands here
# and having them work. Then copy stuff into /working_dir/artifacts .
COMMANDS

#4楼

Mount a volume, copy the artifacts, adjust owner id and group id: 挂载卷,复制工件,调整所有者标识和组标识:

mkdir artifacts
docker run -i --rm -v ${PWD}/artifacts:/mnt/artifacts centos:6 /bin/bash << COMMANDS
ls -la > /mnt/artifacts/ls.txt
echo Changing owner from \$(id -u):\$(id -g) to $(id -u):$(id -u)
chown -R $(id -u):$(id -u) /mnt/artifacts
COMMANDS

#5楼

As a more general solution, there's a CloudBees plugin for Jenkins to build inside a Docker container . 作为更通用的解决方案, Jenkins有一个CloudBees插件可以在Docker容器中构建 You can select an image to use from a Docker registry or define a Dockerfile to build and use. 您可以从Docker注册表中选择要使用的映像,也可以定义要构建和使用的Dockerfile。

It'll mount the workspace into the container as a volume (with appropriate user), set it as your working directory, do whatever commands you request (inside the container). 它将工作空间作为卷(使用适当的用户)安装到容器中,将其设置为工作目录,执行您请求的任何命令(在容器内部)。 You can also use the docker-workflow plugin (if you prefer code over UI) to do this, with the image.inside() {} command. 您也可以通过image.inside(){}命令使用docker-workflow插件(如果您更喜欢UI而不是代码)来执行此操作。

Basically all of this, baked into your CI/CD server and then some. 基本上,所有这些都放入您的CI / CD服务器,然后再放入其中。


#6楼

I am posting this for anyone that is using Docker for Mac. 我将其发布给使用Docker for Mac的任何人。 This is what worked for me: 这对我有用:

 $ mkdir mybackup # local directory on Mac

 $ docker run --rm --volumes-from <containerid> \
    -v `pwd`/mybackup:/backup \  
    busybox \                   
    cp /data/mydata.txt /backup 

Note that when I mount using -v that backup directory is automatically created. 请注意,当我使用-v挂载时,会自动创建backup目录。

I hope this is useful to someone someday. 我希望有一天对某人有用。 :) :)

发布了0 篇原创文章 · 获赞 72 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105224470