简体   繁体   English

Jenkins没有在Docker中启动(包括Dockerfile)

[英]Jenkins not starting in docker (Dockerfile included)

I am attempting to build a simple app with Jenkins in a docker container. 我正在尝试在Docker容器中使用Jenkins构建一个简单的应用程序。 I have the following Dockerfile: 我有以下Dockerfile:

FROM ubuntu:trusty

# Install dependencies for Flask app.
RUN sudo apt-get update
RUN sudo apt-get install -y vim
RUN sudo apt-get install -y curl 
RUN sudo apt-get install -y python3-pip
RUN pip3 install flask

# Install dependencies for Jenkins (Java).
        # Install Java 1.8.
RUN sudo apt-get install -y python-software-properties debconf-utils
RUN sudo apt-get install -y software-properties-common
RUN sudo add-apt-repository -y ppa:webupd8team/java
RUN sudo apt-get update
RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
RUN sudo apt-get install -y oracle-java8-installer
        # Install, start Jenkins.
RUN sudo apt-get install -y wget
RUN wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | apt-key add -
RUN echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list
RUN sudo apt-get update
RUN sudo apt-get install -y jenkins
RUN sudo /etc/init.d/jenkins start

COPY ./app /app

CMD ["python3","/app/main.py"]

I run this container with the following: 我使用以下命令运行此容器:

docker build -t jenkins_test .
docker run --name jenkins_test_container -tid -p 5000:5000 -p 8080:8080 jenkins_test:latest

I am able to start flask and install Jenkins, however, when running, Jenkins is not running. 我能够启动flask并安装Jenkins,但是,在运行时,Jenkins没有运行。 curl localhost:8080 is not successful. curl localhost:8080不成功。

In the log output, I am able to see: 在日志输出中,我可以看到:

Correct java version found
 * Starting Jenkins Automation Server jenkins [ OK ] 

However, it's still not running. 但是,它仍然没有运行。

I can ssh into the container and manually run sudo /etc/init.d/jenkins start to start it, but I want it to start on docker run or docker build . 我可以ssh进入容器并手动运行sudo /etc/init.d/jenkins start来启动它,但是我希望它在docker rundocker build上启动。

I have also tried putting sudo /etc/init.d/jenkins start in the CMD portion of the Docker file: 我也尝试过将sudo /etc/init.d/jenkins start放在Docker文件的CMD部分中:

CMD python3 /app/main.py; sudo /etc/init.d/jenkins start

With this, I am able to curl Flask, but still not Jenkins. 有了这个,我可以卷曲Flask了,但仍然不能让Jenkins卷曲。

How can I get Jenkins to start automatically? 如何让Jenkins自动启动?

You have some points that you need to be aware of: 您需要注意以下几点:

  1. No need to use sudo as the default user is root already. 无需使用sudo因为默认用户已经是root。
  2. In order to run multiple service in the same container you need to use any kind of service manager like Supervisord . 为了在同一容器中运行多个服务,您需要使用任何类型的服务管理器,例如Supervisord Jenkins is not running because the CMD is the main entry point for your container so only flask should be running. Jenkins未运行,因为CMD是容器的主要入口点,因此仅应运行烧瓶。 Check the following link in order to know how to start multiple service in docker. 检查以下链接以了解如何在docker中启动多个服务。

    RUN will be executed only during the build process unlike CMD which will be executed each time you start a container from that image. CMD不同, RUN将仅在构建过程中执行,而CMD每次从该映像启动容器时都将执行。

  3. Combine all the RUN lines together as possible in order to minimize the build layers which lead to a smaller docker image. 尽可能将所有RUN行组合在一起,以最大程度地减少生成层,从而减少码头工人映像。

Regarding the usage of this: 关于此的用法:

CMD python3 /app/main.py; sudo /etc/init.d/jenkins start

It does not work for you because this command python3 /app/main.py is not running as a background process so this command sudo /etc/init.d/jenkins start wont run until the previous command is done. 它对您不起作用,因为此命令python3 /app/main.py没有作为后台进程运行,因此直到上一个命令完成后,该命令sudo /etc/init.d/jenkins start才会运行。

I was only able to get this to work by starting Jenkins in the CMD portion, but needed to start Jenkins before Flask since Flask would continuously run and the next command would never execute: 我只能通过在CMD部分中启动Jenkins使其工作,但是需要在Flask之前启动Jenkins,因为Flask会连续运行并且下一条命令将永远不会执行:

Did not work: 不工作:

CMD python3 /app/main.py; sudo /etc/init.d/jenkins start

This did work: 这确实有效:

CMD sudo /etc/init.d/jenkins start; python3 /app/main.py

EDIT: 编辑:

I believe putting it in the RUN portion would not work because container would build but not save the any running services. 我相信将其放入RUN部分将无法正常工作,因为容器可以构建但不能保存任何正在运行的服务。 I'm not sure if containers can be saved and loaded with running processes like that but I might be wrong. 我不确定容器是否可以保存和加载正在运行的进程,但是我可能是错的。 Would appreciate clarification if so. 如果是这样,请您澄清一下。

It seems like a thing that should be in RUN so if anyone knows why that didn't work or some best practices, would also appreciate the info. 似乎应该RUNRUN因此,如果有人知道为什么这样做不起作用或某些最佳做法,也希望您能了解该信息。

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

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