This post explains running jenkins in docker as a container and persisting the jenkins docker using docker volume. We will also explore using docker-compose to simplify the process.
Terms
jenkins- a ci/cd tooldocker- container management platformdocker-compose- tool to easily manage docker containers
Prerequisites
To get follow along, you need the following:
- Computer with
dockeranddocker-composeinstalled - Access to terminal
- Access to run
dockeranddocker-composein the instance - Willingness to learn
Running Jenkins in docker
For the jenkins to run in docker, the jenkins docker image should exist in local machine. You can either pull the image manually using docker pull or let docker download the image on the fly from docker hub on the fly.
To run jenkins in docker, we can either:
- Run it in the command line with using
docker runcommand - Use
docker-composeby adding the instructions in thedocker-compose.yamlfile
Running with docker-run
Use the command below to run jenkins on docker in the terminal:
| |
From the above command:
- The
--nameflag is used to assign the container created a friendly name. If not provided, a random name will be used. The-nflag can also be used insead of--namelike-n my-jenkins. You can update this to any name you wish to call your jenkins container like this--name different-name - The
-pcommand is used to specify port mapping, i.e. we want the container port to be exposed so it appears to be running locally. In the command, we are exposing container port 8080 where jenkins runs to out local machine so we can accesss it locally likehttp:127.0.0.1:8080/. The format is-p [host-port:container-port]. You can change thehost-portto any port you want if for any reason you don’t want to use port8080 - the
jenkins/jenkins:latestspecifies the image to be used to run the container.
When you run the command above, docker will check if the image exist locally before creating it. If it doesn’t, it will pull the image first.
Result:
| |
Once the container is up and running, verify by checking docker processes using the docker ps command:
| |
To access the Jenkins service, visit http://127.0.0.1:8080 in your page. You will be prompted to do the initial jenkins set up by providing initial jenkins password, Adding plugins you want, and then setting up initial user and specifying the jenkins url.
jenkins data
The above jenkins will run fine. The only problem is containers are stateless and when its either stopped or if the host machine restarts and the data will be lost.
Jenkins stores its data in its home, the JENKINS_HOME env var is exported and its value is the current jenkins home. We can log in to the container and investigate this:
| |
There is also a shorthand to run the command in a non interactive version without logging in to the container:
| |
Persisting Jenkins Data
To persist the jenkins data, you can either copy it to your host machine so you be copying it back when you bring up a new instance. docker cp is used to copy data from and to the container.
This is the format
| |
Command:
| |
Using docker volumes
Docker volume allows you to persist data generated by and used by Docker containers.
The -v command line option can be used with docker run to specify directories to be mounted locally. We can use the following command to mount the jenkins home /var/jenkins_home to the local ./jenkins_home firectory.
| |
Confirming
| |
To know where in the filesystem is the jenkins_home location can be found, use the docker volume command.
| |
Running Jenkins with Docker Compose
With Docker Compose, you can easily deploy one or more instances of Jenkins. You need to create a Docker Compose file and have docker-compose command installed.
The Docker Compose expects a docker-compose.yaml file by default in the current working directory but you can use a different name for the yaml file then use -f <file-name>.yaml to instruct docker-compose to use it.
This is a docker-compose.yaml file to launch jenkins:
| |
In the above:
version- is the docker-compose versionservices- defines the containers we want to launchimage- base image for creating docker container instancecontainer_name- is the friendly name for our containerports- for the ports exposed. Its the port mapping between the Docker host and the containervolumes- port mapping between the Docker host and the
Starting the jenkins instance
use the command below
Start the instance in daemon mode -d. This command docker-compose up -d:
| |
Check docker processes using the docker-compose ps command
| |
Get the initial password:
| |
Stopping jenkins when needed:
| |
Cleaning up
Stop and remove the docker container
| |