Docker & Jenkins: Ensuring Data Persistence and Secure User Management

Hafsah Robleh
Women in Technology
6 min readJul 27, 2023

Overview

In this Docker-based project, I deployed Jenkins, a powerful CI/CD tool, to streamline the software development processes. The primary objectives for this project were to guarantee data persistence for our volumes.

In this project, We’ll pull the Jenkins image from Docker Hub, create a Docker volume for data storage, and run the container with the correct port mapping.

After retrieving the Admin password, we’ll log in and create a new user. Finally, we’ll launch another Jenkins container using the same volume to verify data persistence by logging in as the new user. This ensures a reliable Jenkins environment for continuous integration and development workflows.

Prerequisites

To follow along in this project you’ll need the following:

  • A Docker environment setup
  • An IDE, I’m using Visual Studio code

That's about it, Let's get started!

Pulling the image

So to pull the Jenkins image, I’m going to head over to Docker Hub and search for it.

As you can see, this image is deprecated and instead we can use the image jenkins/jenkins. So let's search that up.

When you click on the image page, on the right-hand side will be its Docker pull command, ready for you to copy.

I’ll also be adding the sudo command along with it. This is to override any permission-denied errors.

sudo docker pull jenkins/jenkins 

Looks like it worked! Lets double check by using the sudo docker image lscommand.

First step down!

Creating the Docker volume

This is the command template for creating Docker volumes, so just replace it with your desired volume name. As well as the sudo command in the beginning to override any permission-denied errors.

docker volume create <volume-name>

You should get your volume name returned back to you. Done.

Mapping out the ports and running the container with the volume

In order to find the correct ports to use, navigate back to the jenkins/jenkins docker hub page. In it, you’ll find a link to the documentation page.

In the last part of the first section, “Usage’’, You’ll find the following command.

This is everything we need along with some extra commands that we will erase. We can erase the -p 50000 part along with the version tags at the very end. As well as the “— restart=on-failure”.

Also, change the “- v jenkins_home:” to your volume name, while keeping the -v. So for me, it looked like this,

docker run -d -v jenkins-volume:/var/jenkins_home -p 8080:8080 jenkins/jenkins

Note: Again, try using the sudo command in the beginning to override any permission errors.

I then used the docker ps command to check if it was running.

It's up!

Retrieving the Admin password

To retrieve the admin password, I’m going to be using the docker logs command as well as the container id.

We got it!

Logging in

Due to the fact that I am running Docker on an EC2 instance, I’ll need the public IP address of my instance to log in. However, If you are running docker on your local environment, try localhost:8080 in your web browser.

In my AWS console, I opened up my Ec2 instance summary,

I then copied this address into my web browser and added “:8080” since this is the port we used.

I kept getting a timeout error.

My immediate thought was of my security group settings, as this is what controls traffic. Before that though, I tried reloading the page and checking my wifi settings. That didn’t work so I went into my security group settings and checked my inbound traffic.

I was allowing HTTP traffic as well as ssh. I know that port 8080 is used by the protocol TCP, so let's add that.

Now to refresh the page.

Yes!! Now to put in the password.

I’m going to go with the suggested plugins, the screen should now look like this.

Logging in and creating a new User

After the initial start-up, Jenkins will bring you to this page,

After configuring a username, password, etc, I then was brought to the homepage.

On the left-hand side is the option for managing Jenkins, click that.

After that click on users.

Click the plus sign and create the user!

Launching a new Jenkins Container with Shared Volume

First, we need to stop our running container, to do this I’ll be using the docker container stop command along with the container id.

By using the arrow keys, I was able to go up into all the previous commands I used and found the one I used to create the container and then added a name for it.

Now, refresh the Jenkins page, and sign in using the new user credentials.

and I’m in! Thats it! We just confirmed that our volume persisted.

Remember to use the docker prune command to take down everything

sudo docker system prune

and for the volume

sudo docker system prune --volumes 

Thank you so much for reading through my article, I appreciate it! I hope you found this documentation helpful. For more project documentation and how-to’s, follow me! I post DevOps projects regularly.

I also have a LinkedIn! Feel free to connect with me, https://www.linkedin.com/in/hafsahrobleh/

--

--