How to run Jenkins using Docker on an AWS EC2 Ubuntu instance. Step-by-step DevOps Guide.

Andrey Byhalenko
DevOps Manuals and Technical Notes
6 min readOct 25, 2023

A beginner’s DevOps guide to installing and running Jenkins using Docker on an AWS EC2 Ubuntu instance.

Note that this guide is explaining how to install Jenkins on an AWS EC2 Ubuntu instance for the purpose of practice only.
This is NOT a production setup guide.

Prerequisites:
AWS account, sign up here —
AWS Console — Signup (amazon.com).

Jenkins — an open source automation server which enables developers around the world to reliably build, test, and deploy their software — jenkins.io.

In order to run Jenkins on an EC2 instance with Docker, you need to follow the next steps:

  • Create an EC2 Linux instance.
  • Install Docker on an EC2 instance.
  • Run Jenkins on Docker.
  • Access Jenkins from your browser.

I’m using a Windows 10 PC, but the process is more or less the same on a Mac.

Minimum hardware requirements for Jenkins:

- 256 MB of RAM
- 1 GB of drive space (although 10 GB is a recommended minimum if running Jenkins as a Docker container)

Create an EC2 Linux instance:

Login to the AWS Console, proceed to the EC2 service, and click on the Instances button from the left menu.
Click “Launch instances” on the top right corner.

Name your instance with the logical name, which reflects the purpose of the server. I chose “docker-jenkins-server”.

For the image, I chose Ubuntu 22.04, Free Tier Eligible.

Next, you need to create a new key pair in order to access the instance from the remote PC (which is in fact our local PC).
Click on “Create new key pair," name it, and click “Create key pair.”

Once the key has been created, you will see the aws-docker-jenkins-server-key.pem file in the Downloads folder (or any default folder for downloads). You can move this file to any location on your PC, just remember where, you will need it later.

Next, you need to create a Security Group under the network settings (AWS Security Groups). Choose Allow SSH traffic from my IP only.

That’s it. Click on the “Launch instance” button and wait until it’s running.

Now that you have the instance running, let’s install Docker on it. In order to connect to the instance from your local PC, click on the check box near the instance and click on "Connect.”

Follow the instructions.

In order to give the necessary permissions to the *.pem file on Windows, open the PowerShell, change the location to the aws-docker-jenkins-server-key.pem file’s folder, and run the following commands:

# reset pem file properties
icacls.exe “aws-docker-jenkins-server-key.pem” /reset
# grants read-only access to the file
icacls.exe “aws-docker-jenkins-server-key.pem” /grant:r “$($env:username):(r)”
# Disable the inherited permissions
icacls.exe “aws-docker-jenkins-server-key.pem” /inheritance:r

Those commands are similar to the chmod 400 aws-docker-jenkins-server-key.pem command on Linux.

Run ssh -i “aws-docker-jenkins-server-key.pem” ubuntu@ec2–*–*–*–*.eu-west-1.compute.amazonaws.com

Once you have connected to the EC2 instance, install Docker on it.

I installed it using the apt repository.

  • Set up Docker’s apt repository:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
  • Install the Docker packages.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • Verify that the Docker Engine installation is successful by running the hello-world image:
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.

Next, let’s run Jenkins using Docker:

Run the following command (change the volume path if needed):

sudo docker run -p 8080:8080 -p 50000:50000 -v /home/ubuntu:/var/jenkins_home jenkins/jenkins

In the console output, you see that Jenkins is running.
Save the password; you will need it to access Jenkins.

You can run the Jenkins Docker image in detached mode (run the container in the background and print the container ID). That means that even if you close the ssh connection to the EC2 instance, the Jenkins Docker image on that server will keep running.

sudo docker run -d -p 8080:8080 -p 50000:50000 -v /home/ubuntu:/var/jenkins_home jenkins/jenkins

If you need Jenkins to be able to use the host’s docker binary, add to the docker run command -v /var/run/docker.sock:/var/run/docker.sock.

In order to access Jenkins from your local browser, you need to proceed to the public IPv4 address and port 8080.
Click on the instance ID in order to see the public IPv4 address.

Now, if you try to proceed to public-IPv4:8080 on your browser, you will see an error.

In order to solve it, you need to add a rule to the Security Group you are using for the EC2 instance.

Proceed to Instance, Security Groups, and Edit inbound rules:

Add rule as below:

Reload the web browser page, and you will see the login window:

Enter the password you saved (that one from the docker logs), install the suggested plugins, Create First Admin User, Save, and Continue.

That’s it, you have Jenkins running on an AWS EC2 Ubuntu instance.

If you liked my articles, join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.

As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.

Subscribe here: https://junior-devops-hub.ck.page

--

--

Andrey Byhalenko
DevOps Manuals and Technical Notes

I'm a DevOps Engineer, Photography Enthusiast, and Traveler. I write articles aimed at junior DevOps engineers and those aspiring to become DevOps engineers.