How to configure a dockerfile and docker-compose for Jenkins How to configure a dockerfile and docker-compose for Jenkins jenkins jenkins

How to configure a dockerfile and docker-compose for Jenkins


docker-compose for Jenkins

docker-compose.yml

version: '2'services:  jenkins:    image: jenkins:latest    ports:      - 8080:8080      - 50000:50000    # uncomment for docker in docker    privileged: true    volumes:        # enable persistent volume (warning: make sure that the local jenkins_home folder is created)        - /var/wisestep/data/jenkins_home:/var/jenkins_home        # mount docker sock and binary for docker in docker (only works on linux)        - /var/run/docker.sock:/var/run/docker.sock        - /usr/bin/docker:/usr/bin/docker

Replace the port 8080, 50000 as you need in your host.

To recreate a new container with the same settings

The volumne mounted jenkins_home, is the placewhere you store all your jobs and settings etc..

Take the backup of the mounted volume jenkins_home on creating every job or the way you want.Whenever there is any crash, run the Jenkins with the same docker-compose file and replace the jenkins_home folder with the backup.

Rerun/restart jenkins again

List the container

docker ps -a

Restart container

docker restart <Required_Container_ID_To_Restart>


I've been using a docker-compose.yml that looks like the following:

version: '3.2'volumes:  jenkins-home:services:  jenkins:    image: jenkins-docker    build: .    restart: unless-stopped    ports:      - target: 8080        published: 8080        protocol: tcp        mode: host    volumes:      - jenkins-home:/var/jenkins_home      - /var/run/docker.sock:/var/run/docker.sock    container_name: jenkins-docker

My image is a locally built Jenkins image, based off of jenkins/jenkins:lts, that adds in some other components like docker itself, and I'm mounting the docker socket to allow me to run commands on the docker host. This may not be needed for your use case. The important parts for you are the ports being published, which for me is only 8080, and the volume for /var/jenkins_home to preserve the Jenkins configuration between image updates.

To recover from errors, I have restart: unless-stopped inside the docker-compose.yml to configure the container to automatically restart. If you're running this in swarm mode, that would be automatic.

I typically avoid defining a container name, but in this scenario, there will only ever be one jenkins-docker container, and I like to be able to view the logs with docker logs jenkins-docker to gather things like the initial administrator login token.

My Dockerfile and other dependencies for this image are available at: https://github.com/bmitch3020/jenkins-docker


HyperV with docker for Windows.

In that case, you must be sure you port-forward any published port (like 5000).

Open HyperV manager, and right-click on the machine defined there: you will be able to add port-forwarding rules in order for localhost:5000 to access your VM:5000.