Run a Jenkins Container In Docker

Yash Tewani
5 min readOct 18, 2023

--

A match containerized in heaven?

Jenkins is an open-source tool that helps automate and streamline various tasks in software development, such as building, testing, and deploying code, making the development process more efficient. The objective of this article is to use Jenkins and to dive further into our understanding of Docker. We will introduce topics such as containerization, persistent volumes, and dockerfiles. This is just another step in our journey.

FOUNDATIONAL: 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.

// PREREQUISITES: Ubuntu Image, VSCode to SSH into EC2 Instance //

First, we must install docker on your instance. Then, we will pull Jenkins from DockerHub. Both can be done with the following commands.

sudo snap install docker
sudo docker pull jenkins/jenkins:lts

Following the installation of Docker, we will create the volume.

sudo docker volume create jenkins-data-volume

Now, we must create/name a container, attach a volume, expose the ports, and specify a file path. We will run the command in the background with ‘-d’. We will map port 8080 on our host to port 8080 in the container, allowing us to access Jenkins via our host machine. In addition, we will map port 50000 for agent communication, enabling the Jenkins master to communicate with its agents. Also, we will link our volume to the directory ‘/var/jenkins_home’ as this will be the storage for Jenkins data. Lastly, we specify Jenkins LTS (Long-Term Support) image to use for the container.

We can also confirm our container is running with the “ps” command.

sudo docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-docker -v jenkins-data-volume:/var/jenkins_home jenkins/jenkins:lts
sudo docker ps

Troubleshooting: If You Are Unable To Access Jenkins's Login Site

When you run a container without specifying a network, Docker typically assigns it to the default bridge network. The default bridge network can sometimes have limitations that affect network communication between your host and the container. In this instance, create a custom network.

sudo docker network create jenkins-network0 
You can type ‘http://localhost:8080’ on VSCode to be prompted, or paste it into your browser

Verify you have reached the Jenkins interface.

We can see Jenkins is successfully running within our docker container. Hoo-rah.

We are prompted to enter Administrative password credentials. To retrieve this, run the following on your terminal, and take note of the password.

sudo docker logs jenkins-docker

When prompted to install Jenkins packages. Select “suggested plugin”.

The second image should prompt that the plugins are being installed successfully. If there is a blank screen then remove Jenkins image and re-do the above steps.

Set up your user account with your desired credentials. After being prompted to a default URL hit continue, and observe Jenkins Dashboard.

The last step is to launch a new Jenkins container using the same volume and verify that the data has persisted by logging in as the newly created user. Let us stop the first container, and then create a second one with the name “Jenkins-dockerv2”. We can do so with the following commands:

sudo docker stop jenkins-docker
sudo docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-dockerv2 -v jenkins-data-volume:/var/jenkins_home jenkins/jenkins:lts
sudo docker ps
With the container running, if we log into Jenkins successfully, this is indeed a persistent volume. Hoo-rah.
Use the same credentials as when we created the user. When complete, observe the same dashboard. We have completed the foundational tier.

ADVANCED: Use Docker Compose

To execute this section of the project, we will use a docker-compose file to take care of our build process and configurations for us.

First, let us remove the image, container, and volume in docker.

sudo docker rm -f <your container name or ID> // command to delete container
sudo docker image rm -f <your image name or ID> // command to delete image
sudo docker volume rm -f <your volume name> // command to delete volume
sudo docker system df //check all docker components taking up disk space. Helpful for monitoring storage.

Now let us create our dockercompose file. Create a directory (here, test1) and in that directory, we will use “vi” to edit our dockerfile.

Note: to start editing in “vi” hit the “i” on your keyboard. When complete, hit “ESC” to exit insert/edit mode. To save changes and exit, type “:wq”.

version: '3'
services:
jenkins-docker:
image: jenkins/jenkins:lts
container_name: jenkins-docker
ports:
- "8080:8080" #Local host port
- "50000:50000" #Jenkins worker node port
volumes:
- jenkins-data-volume:/var/jenkins_home
volumes:
jenkins-data-volume:

After that, we will run “docker up” to execute the file. In this scenario, I had to specify the file path of my dockercompose file.

sudo docker-compose -f /home/ubuntu/test1/dockercompose.yaml  up -d
If done successfully, run localhost:8080 and find yourself back at Jenkins login. This is the end of the advanced tier. Thanks for reading!

--

--

Yash Tewani

Graduated with A.S. and B.S. in Computer Science and Information Security. Interests are rooted in problem solving related to Business and Technology