Running Jenkins as a Docker container with volume using an AWS EC2 instance.

Introduction

This tutorial will be a hands-on demonstration of installing Jenkins as a docker container while using an AWS EC2 instance for the server. To follow along, create an AWS account and you will need a basic knowledge of Docker.

Jenkins is an open-source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool used by both developers and DevOps engineers. It is used to implement CI/CD workflows, called pipelines. It is written in the Java programming language.

Prepare a server using an AWS EC2 instance.

Open your favorite web browser and log in to your AWS account, and click on the EC2 to create a server for Jenkins

Afterward, click on the instances running to show if there is an available instance.

Then, click on 'launch instances' to start a server instance,

Provide a name for the server, and choose the desired operating system for the server. I choose Ubuntu 22.04 free tier and named the server Jenkins.

Create a security key pair to have access to the server or select from the previously created security key.

Click the Finish button to launch the instance and view the launched instance.

Click on the checkbox beside the Jenkins server name, and click on the connect button.

Copy the ssh command to connect to the server and have access from your terminal.

Connect to EC2 server from PowerShell or terminal

Next, open your preferred terminal, PowerShell or terminal based on your operating system, and connect to the server. However, change to the directory of the security key on your laptop so that it is available for the connection. Paste, the ssh command and connect to the server.

Update the Ubuntu server and Install Docker

Now, update the Ubuntu OS to ensure that all softwares are up to date. Use the command below,

sudo apt update

Now, once the updates are installed, then install docker using the command below to be able to run the docker command for installing Jenkins.

sudo apt install docker.io

Install Jenkins using Docker with volume

Once docker is installed, run the docker command below to install the Jenkins container. However, the "-v jenkins_home:/var/jenkins_home" creates a mounted volume where Jenkins stores all the logs for the jobs and activities carried out on it. Also, the "jenkins/jenkins:lts" is the official name of the Jenkins image on the Docker hub website. The default port used by Jenkins is 8080, the host port is 8080 and the container port is the second 8080.

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

Afterward, check if Jenkins is running on the server using the command below:

sudo docker ps

Copy the public IP address of the EC2 server and add the port (8080) to access Jenkins on the browser for configuration. Note: if the installation is not completed at once, the public IP address for the EC2 will change at each start and stop of the instance. The path shown in the browser highlighted in red is the location for the initial password to continue the installation.

Access the initial password for Jenkins by going back to the console and logging in to the Jenkins user for the password.

Copy the password from the terminal and paste it into the Administrative password textbox on the browser, then click Continue.

Configure Jenkins and the admin user access

Select the install suggested plugins button. These installs all the community-recommended plugins. However, if you have specified plugins to install, click the select plugins to install.

All the recommended plugins are installed, this may be fast or slow depending on your internet connection.

Once the plugin installations are completed, a page to Create First Admin User is shown. Fill in all the fields with the required information and click on Save and Continue. Make sure to keep the details of the credential as they will be used to sign in to the Jenkins server.

The next page shown is the instance configuration. This shows the URL for the Jenkins admin page. The URL provided is for the Jenkins user. If you are using an AWS EC2 server, the address will be http://<public-address>:8080 or localhost:8080. Click on Save and Finish to continue.

The final setup page shows Jenkins is ready. Click on Start using Jenkins and the Welcome page is shown.

Sign in with the credentials created previously to have access to the Jenkins web portal. From there, you can start creating Jenkins jobs or build pipelines.

Thank you for reading. Please follow, like, comment, and share for a wider reach and more DevOps-related articles.