Do I run Jenkins as a docker container? In this guide we are going to explore how to achieve that using docker and simplifying the process using docker compose.
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Docker Compose file provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command
Docker runs processes in isolated containers. A container is a process which runs on a host. The host may be local or remote. When an operator executes docker run
, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.
Prerequisites
To follow along, ensure that you have the following:
- Docker installed locally or in the host server that you are using
- Access to docker and ability to run docker commands that create containers
- Internet access to download docker images
Running using docker run
command
So I want to run the latest version of Jenkins as a container in a detached mode, while mounting the Jenkins home path so the data can persist restarts.
|
|
This is an explanation of the above used flags:
-d
instructs docker to run the container in a detached mode. If this command is not used, the container will be run as a blocking process in the current terminal session.--name
gives the container a meaningful name. If it is not used a random name will be used.-p
this is used to mount the container ports to the host. In our case, the container port8080
is mapped to the localhost8099
--restart
argument is used to instruct docker to restart the container if it fails for any reason. The value suppliedon-failure:10
means that the container will only be restarted if there is a failure and that it will only happen 10 times before docker gives up.-v
is used to map container paths to local path. Containers are ephemeral services that don’t persist state. When a container stops or restarts all the data will be lost. Mounting a volume this way ensures that the data is saved in the local path that can be re-used when the container restarts. In our case, we are mounting Jenkins home/var/jenkins_home
where Jenkins data is stored to local path~/apps/jenkins
.- Lastly, the
jenkins/jenkins:latest
defines the image to be used. We are using thejenkins/jenkins
image, fetching the latest tag. If you have a specific version of jenkins that you want to use you can specify it withjenkins/jenkins:<version>
likejenkins/jenkins: 2.315-alpine
This is the output of the above command in my terminal:
|
|
Now that the container is running, let us check the processes:
|
|
Logging in and running commands:
|
|
Or:
|
|
Tail follow logs:
|
|
Cleaning up
First, stop the container:
|
|
Then remove the stopped container:
|
|
Running using docker-compose
command
This docker-compose yaml code achieves the same functionality in a codified way. This is advantageous as you can check it in a source code management system.
Save it as docker-compose.yml
|
|
Bringing up
|
|
This will bring up the Jenkins container in a detached mode.
Check docker processes:
|
|
To execute commands, use the exec
. Example how to get the default password:
|
|
Or:
|
|
Tail follow logs:
|
|
Cleaning up docker compose
Stop the containers:
|
|
Then remove them, you will be prompted to confirm:
|
|
Accessing the Jenkins UI
Now that we have our Jenkins running, we can access it in the UI using the server or local IP and the port that we mounted (in our case 8099).
Once the server is up, use the command to get the password and click Continue.
|
|
You should be able to set up and use your Jenkins now.
Conclusion
Up to this point, we managed to set up Jenkins using docker while persisting the data in a locally mounted volume.