20 Aug

Run Jenkins in the ​container

The more you work with automation, the more you will like the containers. They fit and scale correctly in CI/CD model and can be easily managed. The whole infrastructure for automation should be flexible, easy to maintain and extendable – containers fit perfectly into this model. So why not start from putting Jenkins in the container?

Benefits of running Jenkins in the container

Using the containers to run Jenkins includes all of the benefits of containers like scalability or security, but there are some additional ones if you provide proper configuration of two components:

  • Dedicated networks for connections to other local resources
  • Static volumes for specific folders 

As another local resource, you can count you local Docker Repository where you store container images. Dedicated network, with no external exposure, will provide a secure channel to send images built in Jenkins pipeline to the repository. It can be local bridged network or the swarm depending on the infrastructure setup. The same way access to local GIT repository may be provided.

The static volumes are required to preserve some data when you remove or rebuild the container. The static volumes should cover at least folder /var/jenkins_home. This is the home directory where Jenkins stores all the dynamic components, but not its binaries. You will find there all projects, configuration, logs, and plugins

Such separation when you run Jenkins in containers is handy. To upgrade Jenkins, you need to download a new image from the repository and rebuild the container – while the software will change the configuration and plugins will remain unchanged. The official upgrade procedure includes downloading and installing the new .war file so rebuilding the container is much easier and faster. You can also rollback to any previous version easily.

Running Jenkins in Docker

Jenkins developers maintain official repository on Docker Hub as jenkins/jenkins. You will find there two lines of the software releases. The LTS (Long Term Support) line provides the stable images dedicated to run in the production networks. A new stable release is published in about every twelve weeks – to access it you need to refer to the lts tag on docker hub – jenkins/jenkins:lts. Second like you can call unstable or development with a new release every week. To access it use the tag latest.

I do not like to start and maintain containers via docker command line no matter if the project consists of single or multiple containers. I prefer to use docker-compose for every single deployment. Let’s look at the most straightforward possible configuration file

version: '3'
services:
  jenkins:
    image: 'jenkins/jenkins:latest'
    restart: unless-stopped
    ports:
      - '50080:8080'
      - '50443:8443'
      - '50000:50000'
    volumes:
      - 'jenkins_home:/var/jenkins_home'
volumes:
  jenkins_home:
    driver: local

As described previously I use local volume for /var/jenkins_home folder. Depending if you are using HTTP or HTTPS to connect to Jenkins you need to expose externally ports 8080 or 8443 or both. Port 50000 is required for remote API.

You can find examples of docker-compose.yml configuration files to run Jenkins in the container on my repository. I wrote about it in my blog post https://blog.it-playground.eu/repository-with-docker-templates/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: