如何使用 jenkins docker 容器挂载 docker 卷? [英] How to mount docker volume with jenkins docker container?

查看:60
本文介绍了如何使用 jenkins docker 容器挂载 docker 卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 github 上的容器和项目源代码中运行 jenkins.

I have jenkins running inside container and project source code on github.

我需要在与 jenkins 相同的主机上的容器中运行项目,但不是作为 docker-in-docker,我想将它们作为同级容器运行.

I need to run project in container on the same host as jenkins, but not as docker-in-docker, i want to run them as sibling containers.

我的管道如下所示:

  1. 从github拉源码
  2. 构建项目映像
  3. 运行项目容器

我现在做的是使用 jenkins 容器中主机的 docker 套接字:

What i do right now is using the docker socket of host from jenkins container:

/var/run/docker.sock:/var/run/docker.sock

当 jenkins 容器将带有源代码的卷从/var/jenkins_home/workspace/BRANCH_NAME 挂载到项目容器时遇到问题:

I have problem when jenkins container mount the volume with source code from /var/jenkins_home/workspace/BRANCH_NAME to project container:

volumes:
 - ./servers/identity/app:/srv/app

我在项目容器中得到空文件夹/srv/app"

i am getting empty folder "/srv/app" in project container

我最好的猜测是 docker 尝试从主机而不是从 jenkins 容器中挂载它.

My best guess is that docker tries to mount it from host and not from the jenkins container.

所以问题是:如何显式设置我安装卷的容器?

So the question is: how can i explicitly set the container from which i mount the volume?

推荐答案

我在使用 Jenkins docker 容器运行另一个容器时遇到了同样的问题.

I got the same issue when using Jenkins docker container to run another container.

这不是推荐的方式,解释 到这里.如果你还需要使用这种方式,那么这个问题就不是问题了.

This is not a recommended way, explanations goes here. If you still need to use this approach, then this problem is not a problem.

假设,我们需要在 Jenkins docker 容器中运行另一个容器 (ContainerA),docker pipeline plugin 将使用 --volumes-fromJenkins 容器卷 挂载到 ContainerA.

Suppose, we need to run another container (ContainerA) inside Jenkins docker container, docker pipeline plugin will use --volumes-from to mount Jenkins container volume to ContainerA.

如果您尝试使用 --volume-vJenkins 容器 中的特定目录映射到 ContainerA,你会得到一个意想不到的行为.

If you trying to use --volume or -v to map specific directory in Jenkins container to ContainerA, you will got an unexpected behavior.

这是因为 --volumes-v 会尝试将 host 中的目录映射到 ContainerA,而不是比从 Jenkins 容器 内的目录映射.如果在 host 中找不到目录,那么您将在 ContainerA 中获得一个空目录.

That's because --volumes or -v would try to map directories in host to ContainerA, rather than mapping from directories inside Jenkins container. If the directories not found in host, then you will get an empty dir inside ContainerA.

简而言之,我们不能将特定目录从 containerA 映射到 containerB,我们只能将整个卷从 containerA 挂载到 containerB,并且 不支持卷别名.

In short, we can not map a specific directory from containerA to containerB, we could only mount the whole volumes from containerA to containerB, and volume alias is not supported.

  1. 如果您的 Jenkins 使用主机卷运行,您可以将主机目录映射到目标容器.
  2. 否则,您可以访问与 Jenkins 容器相同位置的新创建容器内的文件.
  1. If your Jenkins is running with host volume, you can map the host directories to the target container.
  2. Otherwise, you can access the files inside the newly created container with the same location as Jenkins container.

这篇关于如何使用 jenkins docker 容器挂载 docker 卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆