Jenkins and Docker: The Dynamic Duo

Ebony Moorer
5 min readJan 16, 2024

--

Imagine Jenkins as your diligent personal assistant in the software realm. Jenkins handles all of your needs — building and testing code to organizing and deploying applications. Jenkins is reliable and ensures everything runs smoothly in the background, allowing you to focus on the creative aspects of your work.

Docker, is like a handyman. Docker is a toolbox that neatly packs your application and its dependencies into portable containers. These containers ensuring your app can be crafted and fine-tuned from anywhere, be it your local machine or a remote server. Where do volumes come into play? They act as additional storage rooms. Volumes allow you to store data outside the containers, making sure your project materials are securely stored and easily accessible.

Jenkins is great for coordinating tasks and while Docker provides the flexible workspace for your applications. Together, Jenkins and Docker make a killer combo. Jenkins orchestrates the whole development lifecycle, and Docker ensures your app runs consistently in any environment.

For this use case of Docker, I will be following these steps to showcase how all three of this components work together.

Using the CLI

  1. Pull the Jenkins LTS image from Docker Hub.
  2. Create a Docker volume for Jenkins to persist its data.
  3. Run the container with the volume and correct port mapped to the container.
  4. Retrieve the Admin password.
  5. Login and create a new user.
  6. Launch a new Jenkins container using the same volume and verify that the data has persisted by logging in as the newly created user.

Jenkins Image, Creating A Volume in Docker and Running A Container

In a previous project, I created a friendly guide on using docker and pulling images from Dockerhub to run on Docker and creating a container. I use VS Code to complete these tasks as well as an EC2 instance through AWS using Ubuntu as my AMI image. To ensure you can open your web browser, once you have setup your EC2 instance, make sure to have port 8080 configured in your security groups on your instance through AWS EC2 by following these steps:
Add Rule for Port 8080 (Jenkins Web Interface):

  • Set the following parameters for the new rule: Type: Custom TCP |Rule Protocol: TCP |Port Range: 8080 | Source: 0.0.0.0/0 (This allows traffic from any IP address. Adjust as needed based on your security requirements.)

I will follow similar steps but will add the coding for the steps to show the extraction of files for Jenkins. Searching up Jenkins on Dockerhub will allow you to find the command needed to install Jenkins.

Next, run the volume create command to create your volume and your volume will quickly be created!

Lastly, run the container with the volume attached and the correct port mapping:

  • -d: Runs the container in the background.
  • -p:8080:8080: Map host port 8080 to container port 8080 (Jenkins web interface).
  • -p 50000:50000: Map host port 50000 to container port 50000 (Jenkins agent communication).
  • -v jenkins_data:/var/jenkins_home: Mount the created volume to persist Jenkins data.
sudo docker pull jenkins/jenkins:lts
sudo docker create volume nameyourvolume
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_data:/var/jenkins_home --name jenkins_container jenkins/jenkins:lts

Retrieve Admin Password and Login To Create New User

In order to complete this step, you can use this command to retrieve the password.

Login and create a new user:

  • Open a web browser and navigate to https://localhost:8080. In this case, the “localhost” would be the IP address of your EC2 instance.
  • Enter the Admin password obtained in the previous step.
  • Follow the on-screen instructions to complete the Jenkins setup.
  • Create a new user through the Jenkins web interface.
sudo docker exec practice_jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Launch New Container To Verify Volume Data Persists

This step is to verify your container is running, so that it can be stopped and that the volume still “persists”. I chose to stop the container and verify that it had stopped running. When I checked the IP address, the browser gave me the error that the host couldn’t be reached.

#to check running containers
sudo docker ps -a

#to stop container
sudo docker container stop <containerid>

Create a new container, and use the same public address to verify that the container works!

--

--