[英]How to run Docker inside Jenkins which is running as container
I'm working on Centos7. 我正在使用Centos7。 I have a Docker container which is running Jenkins.
我有一个运行Jenkins的Docker容器。 In that Jenkins-container I have to build and run other Docker containers.
在Jenkins容器中,我必须构建并运行其他Docker容器。 But Jenkins doesn't know docker.
但詹金斯不知道码头。 I'm able to execute a shell and install docker inside the container.
我能够执行shell并在容器内安装docker。 But isn't it possible to let the container use my docker-engine on the host?
但是不可能让容器在主机上使用我的docker-engine吗? How can I use it?
我怎么用呢?
What is the best option to install Docker inside a Jenkins-(docker)-container? 在Jenkins-(docker)-container中安装Docker的最佳选择是什么?
Generally, a container-in-container setup involves linking /var/run/docker.sock
and docker
itself. 通常,容器中容器设置涉及链接
/var/run/docker.sock
和docker
本身。
For example, in this thread : 例如, 在这个帖子中 :
docker run --name jenkins --privileged=true -t -i --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/bin/docker -p 8080:8080 jenkins
This is not exactly your case, since you don't need to run Jenkins itself in a "cic" (container in container"). 这不完全是你的情况,因为你不需要在“cic”(容器中的容器“)中运行Jenkins本身。
But that illustrates how you would run any container in a container, with docker available in it. 但这说明了如何在容器中运行任何容器,其中包含docker。
Make sure the user in that container is part of the docker group (if you don't want to use root), as in this jenkins/setup-docker-and-start-jenkins.sh
script 确保该容器中的用户是docker组的一部分(如果你不想使用root),就像在这个
jenkins/setup-docker-and-start-jenkins.sh
脚本中一样
#!/bin/sh
set -e
JUSER="jenkins"
DOCKER_GID=$(ls -aln /var/run/docker.sock | awk '{print $4}')
if ! getent group $DOCKER_GID; then
echo creating docker group $DOCKER_GID
addgroup --gid $DOCKER_GID docker
fi
if ! getent group $GID; then
echo creating $JUSER group $GID
addgroup --gid $GID $JUSER
fi
if ! getent passwd $JUSER; then
echo useradd -N --gid $GID -u $UID $JUSER
useradd -N --gid $GID -u $UID $JUSER
fi
DOCKER_GROUP=$(ls -al /var/run/docker.sock | awk '{print $4}')
if ! id -nG "$JUSER" | grep -qw "$DOCKER_GROUP"; then
adduser $JUSER $DOCKER_GROUP
fi
chown -R $JUSER:$JUSER /var/jenkins_home/
Note that this setup uses tini
to launch Jenkins (as I described in " Jenkins does not run automatically after install in Docker container ") 请注意,此设置使用
tini
启动Jenkins(正如我在“ 在Docker容器中安装后Jenkins不会自动运行 ”中所述)
exec su $JUSER -c "/bin/tini -- /usr/local/bin/jenkins.sh"
Again, those scripts are for using Jenkins in "cic". 同样,这些脚本用于在“cic”中使用Jenkins。
In your case, you can use those scripts for the containers that your Jenkins will have to run. 在您的情况下,您可以将这些脚本用于Jenkins必须运行的容器。
Using the official Jenkins docker image: 使用官方詹金斯码头图像:
docker run -d \
-u root \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker:ro \
-p 8080:8080 \
--name jenkins \
jenkins
Then to verify everything is working: 然后验证一切正常:
docker version
as content docker version
作为内容 If you run into the following error on CentOS 7: 如果您在CentOS 7上遇到以下错误:
docker: error while loading shared libraries: libsystemd-journal.so.0: cannot open shared object file: No such file or directory
docker:加载共享库时出错:libsystemd-journal.so.0:无法打开共享对象文件:没有这样的文件或目录
then start the container with: 然后启动容器:
docker run -d \
-u root \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker:ro \
-v /usr/lib64/libsystemd-journal.so.0:/usr/lib/x86_64-linux-gnu/libsystemd-journal.so.0 \
-v /usr/lib64/libsystemd-id128.so.0:/usr/lib/x86_64-linux-gnu/libsystemd-id128.so.0 \
-v /usr/lib64/libdevmapper.so.1.02:/usr/lib/x86_64-linux-gnu/libdevmapper.so.1.02 \
-v /usr/lib64/libgcrypt.so.11:/usr/lib/x86_64-linux-gnu/libgcrypt.so.11 \
-v /usr/lib64/libdw.so.1:/usr/lib/x86_64-linux-gnu/libdw.so.1 \
-p 8080:8080 \
--name jenkins \
jenkins
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.