Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Jenkins in Docker - Exits immediately

I'm trying to run a Jenkins server with docker.

I've built the image, and trying to run it with this command:

docker run -p 8080:8080 62a4e44bf4bf

The 62a4e44bf4bf is the docker image ID

Whenever I run this command, it shuts down immediately.

I've tried with this command:

docker run -i -t -p 8080:8080 62a4e44bf4bf

Which will keep the image running, but I can't seem to access the jenkins from my browser with this ip: localhost:8080

DOCKERFILE:

FROM ubuntu:latest

#Oracle Java7 install
RUN apt-get install software-properties-common -y
RUN apt-get update
RUN add-apt-repository -y ppa:webupd8team/java
RUN apt-get update
RUN echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-get install -y oracle-java7-installer

#Jenkins install
RUN wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
RUN sudo echo "deb http://pkg.jenkins-ci.org/debian binary/" >>     /etc/apt/sources.list
RUN apt-get update
RUN apt-get install --force-yes -y jenkins

#Zip support install
RUN apt-get update
RUN apt-get -y install zip

#Unzip hang.zip
#RUN cp /shared/hang.zip /var/lib/jenkins
#RUN unzip -o /var/jenkins/hang.zip -d /var/lib/jenkins/

#Restart jenkins server
RUN service jenkins start

EXPOSE 8080
like image 545
Detilium Avatar asked May 04 '15 09:05

Detilium


2 Answers

@Detilium There is no CMD or ENTRYPOINT in your Dockerfile, that's why it automatically exits, because your container has nothing to run. According to your own answer, you probably made it work by using exec or running bash and starting it manually, I guess ?

There something wrong with you Dockerfile though. Docker container have to run a process as pid 1 (inside the container) and it has to run in foreground (no daemon).

More over, images reperesent filesystems, not state. if you start a process in a step of your image building (in the Dockerfile), the fact it's running will not be /persisted/, so it won't be started again when you run a container based on this image. The default command that will be runned by docker when starting the container is definde with CMD.

RUN service jenkins start

The line above does not work. It will start jenkins at this step of the build and make a layer (that might be empty even). But the next layer that will be created (another step or the actual docker run) will not have the jenkins service/process running.

For /inspiration/ for a Jenkins Dockerfile, you could look at https://github.com/aespinosa/docker-jenkins/blob/master/Dockerfile . Note the ENTRYPOINT ["java", "-jar", "/opt/jenkins.war"] could also be CMD ["java", "-jar", "/opt/jenkins.war"].

I encourage you to read the following documentation for more comprehension about docker : https://docs.docker.com/userguide/dockerimages/, https://docs.docker.com/reference/builder/ and https://docs.docker.com/articles/dockerfile_best-practices/.

like image 139
Vincent Demeester Avatar answered Sep 23 '22 01:09

Vincent Demeester


It is possible permissions on your jenkins directory are a problem when jenkins container will not stay running. I did 'docker pull jenkins' and was surprised to find it would not run. https://hub.docker.com/_/jenkins/

To debug your error then start the image using the -i (interactive flag). (use 'docker ps -a |grep jenkins' if you don't know your ID)

docker start 62a4e44bf4bf -i

I saw an error like this:

touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

So I checked /var/jenkins_home ownership and permissions. I saw that the $PWD/jenkins dir had been created with root:root 700 permission. I adjusted (Sledgehammer approach): 'sudo chmod 777 $PWD/jenkins'. Problem solved. Container ran ok and ran the install of jenkins into that directory.

I am running jenkins container like this: (PWD=/home/myuser so /var/jenkins_home in container is actually $PWD/jenkins on docker server)

docker run -d -p 49001:8080 -v $PWD/jenkins:/var/jenkins_home:z -t jenkins
like image 20
gaoithe Avatar answered Sep 24 '22 01:09

gaoithe