How to configure a dockerfile and docker-compose for Jenkins

16,412

Solution 1

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

Solution 2

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>
Share:
16,412
chaotic
Author by

chaotic

Updated on June 13, 2022

Comments

  • chaotic
    chaotic almost 2 years

    Im absolutely new in Docker and Jenkins as well. I have a question about the configuration of Dockerfile and docker-compose.yml file. I tried to use the easiest configuration to be able to set-up these files correctly. Building and pushing is done correctly, but the jenkins application is not running on my localhost (127.0.0.1).

    If I understand it correctly, now it should default running on port 50000 (ARG agent_port=50000 in jenkins "official" Dockerfile). I tried to use 50000, 8080 and 80 as well, nothing is working. Do you have any advice, please? Im using these files: https://github.com/fdolsky321/Jenkins_Docker

    The second question is, whats the best way to handle the crashes of the container. Lets say, that if the container crashes, I want to recreate a new container with the same settings. Is the best way just to create a new shell file like "crash.sh" and provide there the information, that I want to create new container with the same settings? Like is mentioned in here: https://blog.codeship.com/ensuring-containers-are-always-running-with-dockers-restart-policy/

    Thank you for any advice.