简体   繁体   English

Docker和詹金斯

[英]Docker and jenkins

I am working with docker and jenkins, and I'm trying to do two main tasks : 我正在使用docker和jenkins,并且正在尝试执行两个主要任务:

  1. Control and manage docker images and containers (run/start/stop) with jenkins. 使用jenkins控制和管理docker映像和容器(运行/启动/停止)。
  2. Set up a development environment in a docker image then build and test my application which is in the container using jenkins. 在docker映像中设置开发环境,然后使用jenkins构建并测试容器中的我的应用程序。

While I was surfing the net I found many solutions : 在网上冲浪时,我发现了许多解决方案:

  • Run jenkins as container and link it with other containers. 将jenkins作为容器运行,并将其与其他容器链接。
  • Run jenkins as service and use the jenkins plugins provided to support docker. 将jenkins作为服务运行,并使用提供的用于支持docker的jenkins插件。
  • Run jenkins inside the container which contain the development environment. 在包含开发环境的容器内运行jenkins。

So my question is what is the best solution or you can suggest an other approach. 所以我的问题是最好的解决方案是什么,或者您可以建议其他方法。

One more question I heard about running a container inside a container. 我听到了另一个有关在容器内运行容器的问题。 Is it a good practice or better avoid it ? 这是一个好习惯还是更好地避免呢?

To run Jenkins as a containerized service is not a difficult task. 将Jenkins作为容器化服务运行并非难事。 There are many images out there that allow you to do just that. 有很多图像可以让您做到这一点。 It took me just a couple minutes to make Jenkins 2.0-beta-1 run in a container, compiling from source (image can be found here ). 我花了几分钟才使Jenkins 2.0-beta-1在容器中运行,并从源代码进行编译(可在此处找到图像)。 Particularity I like this approach, you just have to make sure to use a data volume or a data container as jenkins_home to make your data persist. 特殊性我喜欢这种方法,您只需要确保将数据卷或数据容器用作jenkins_home即可使数据持久化。

Things become a little bit trickier when you want to use this Jenkins - in a container - to build and manage containers itself. 当您想在容器中使用此Jenkins来构建和管理容器本身时,事情变得有些棘手。 To achieve that, you need to implement something called docker-in-docker, because you'll need a docker daemon and client available inside the Jenkins container. 为此,您需要实现一个名为docker-in-docker的东西,因为您需要在Jenkins容器中可用的docker守护程序和客户端。

There is a very good tutorial explaining how to do it: Docker in Docker with Jenkins and Supervisord . 有一个很好的教程解释了如何做到这一点: Docker中的Docker与Jenkins和Supervisord

Basically, you will need to make the two processes (Jenkins and Docker) run in the container, using something like supervisord. 基本上,您将需要使用类似于supervisor的东西使两个进程(Jenkins和Docker)在容器中运行。 It's doable and proclaims to have good isolation, etc... But can be really tricky, because the docker daemon itself has some dependencies, that need to be present inside the container as well. 这是可行的,并宣称具有良好的隔离性,等等。但是这可能确实很棘手,因为docker守护程序本身具有一些依赖关系,因此也需要在容器内部存在。 So, only using supervisord and running both processes is not enough, you'll need to make use of the DIND project itself to make it work... AND you'll need to run the container in privileged mode... AND you'll need to deal with some strange DNS problems... 因此,仅使用主管程序并运行两个进程是不够的,您将需要利用DIND项目本身来使其正常工作...并且您需要在特权模式下运行容器...并且我需要处理一些奇怪的DNS问题...

For my personal taste, it sounded too much workarounds to make something simple work and having two services running inside one container seems to break docker good practices and the principle of separation of concerns, something I'd like to avoid. 就我个人的喜好而言,听起来似乎有太多的解决方法以简化工作,并且在一个容器中运行两个服务似乎违反了Docker的良好做法和关注点分离的原则,这是我想避免的。

My opinion got even stronger when I read this: Using Docker-in-Docker for your CI or testing environment? 当我读到这篇文章时,我的观点变得更加强烈: 在您的CI或测试环境中使用Docker-in-Docker? Think twice . 三思而后行 It's worth to mention that this last post is from the DIND author himself, so he deserves some attention. 值得一提的是,上一篇来自DIND作者本人,因此他值得关注。

My final solution is: run Jenkins as a containerized service, yes, but consider the docker daemon as part of the provisioning of the underlying server, even because your docker cache and images are data that you'll probably want to persist and they are fully owned and controlled by the daemon. 我的最终解决方案是:将Jenkins作为容器化服务运行,是的,但是将docker守护进程视为基础服务器配置的一部分,即使您的docker缓存和图像是您可能想要保留的数据并且它们已经完全保留由守护程序拥有和控制。

With this setup, all you need to do is mount the docker daemon socket in your Jenkins image (which also needs the docker client, but not the service): 使用此设置,您需要做的就是在您的Jenkins映像中安装docker守护进程套接字(它也需要docker客户端,但不需要服务):

$ docker run -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v local/folder/with/jenkins_home:/var/jenkins_home namespace/my-jenkins-image

Or with a docker-compose volumes directive: 或使用docker-compose volumes指令:

---
version: '2'

services:
  jenkins:
    image: namespace/my-jenkins-image
    ports:
      - '8080:8080'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - local/folder/with/jenkins_home:/var/jenkins_home

# other services ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM