Docker

Setup a Jenkins Server Using Docker

Jenkins is a widely used open-source automation server that helps to automate the various parts of the software development process including building, testing, and deploying applications.

Docker is a containerization platform that allows us to run the applications and services in isolated environments known as containers.

In this tutorial, we will learn how to combine Jenkins with Docker which allows us to create a scalable and portable Jenkins’s server.

NOTE: In this tutorial, we will use the official jenkins/jenkins image from the Docker Hub repository. This image contains the current Long-Term Support (LTS) release of Jenkins which is production-ready.

Create a Network

The first step is to create a new Docker network to isolate the Jenkins instance. This facilitates a better communication.

To create a bridged network in Docker, we can run the following command:

$ docker network create jenkins

 
This creates a bridged network called Jenkins.

Run the Jenkins Docker Container

Once we have the network configured, we can run the Jenkins container using the official Jenkins Docker image.

We also specify the network and configure a volume for the Jenkins data persistence. The command is as follows:

docker run \
  --name jenkins-docker \
  --rm \
  --detach \
  --privileged \
  --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376 \
  docker:dind \
  --storage-driver overlay2

 

The previous command launches a Docker-in-Docker (DinD) container named “jenkins-docker” with elevated privileges and networking configurations.

The –rm flag ensures that the container is automatically removed when it stops. The container is connected to the Jenkins network with the network alias as “docker” as created in the previous step.

We then set the environment variables for the Docker TLS certificates and mount the volumes for certificate storage and Jenkins data.

In the next section, we publish port 2376 for the Docker daemon communication.

Lastly, we specify the docker:dind image and configure the storage driver to use the overlay2.

Access the Jenkins Web UI

Once we start the container, we can access the Jenkins instance from a web browser using the http://localhost:8080 address.

If you wish to run a simple Jenkins container, you can use the following command:

$ docker run -d -p 8080:8080 -p 50000:50000 --name jenkins \
  --network jenkins \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

 
This runs the Jenkins server without the need to configure the other features such as volumes, bind ports, and more.

You can then access the Jenkins instance on http://localhost:8080.


Once Jenkins is ready, your browser will allow you to quickly configure the Jenkins server with the properties as you wish.

Once it is setup, you need to unlock Jenkins by specifying the administrator password. You can find it by running the following command:

$ docker logs jenkins

 
The command shows you the logs for the container which includes the Jenkins password.


 

The next step is to choose and install the plugins that you wish to use for your Jenkins instance. If you are using Jenkins for the first time, choose the recommended plugins.


This should allow Jenkins to download and install all the default plugins which allows you to interact with the Jenkins pipelines with ease.

Conclusion

In this tutorial, you learned how you can setup a Jenkins server as container using Docker and the official Jenkins image.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list