Running Jenkins in Docker - Exits immediately Running Jenkins in Docker - Exits immediately jenkins jenkins

Running Jenkins in Docker - Exits immediately


@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/.


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 deniedCan 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


This is an old thread but in my case it was lack of memory that caused the shutdown. I'm running docker on Docker Desktop on a mac. Allocating more memory to Docker Desktop solved the issue. Maybe this can help someone.