Configuring and Running Jenkins on a Remote Server with Docker
Tags: devops, docker
Published: 2021-06-20 03:26:31

In this article, I will go over how you can start Jenkins up on a remote server using the official image from Docker. 

In this tutorial, for our server, I will use DigitalOcean. 

I do want to preface this by saying, running a remote server on DigitalOcean is not free. However, it is relatively cheap to start one up. 

Let's get started!



<hr/>

## Creating/Configuring The Server


1) Create an account with Digital Ocean. 
<ul>
    <li> Type "https://www.digitalocean.com/" in your 
         browser</li>
    <li> Click the "Sign Up" button on the top right of the website. </li>
    <li> You can either create an account with your email, Github, or Google. </li>
    <li> After you create an account, you will need to verify that it's you. </li>
</ul>

![Digital Ocean Sign Up Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le0idp5o3u4koa9fscwg.png)

2) Next, you want to create a Digital Ocean (Droplet)

![Create Droplet Button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/athre87hwye76qu4n4z2.png)

3) After creating a Digital Ocean Droplet, you want to configure some settings. Lets break it down!

First, we will choose an image based on our current distribution. Currently, my machine is Linux Mint, so it is Ubuntu-based. Therefore, I will select "Ubuntu"

![Choose Image based on Distro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pux3pxunp8150c74x25l.png)
 
Second, we will select our plan. I will go with the cheapest and select "Basic".

![Choose Plan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93n7psyknoufs4s4sh3d.png)

Third, in our CPU Options, we will select "Regular Intel with SSD" and select the $20/month plan.

![Choose CPU Options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxlv82i5p9857diyi4d9.png)

Fourth, select the region closest to you.

![Choose Region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq0gj1p05tk3po7vvare.png)

Fifth, we will select our authentication option as SSH.If you don't have an SSH already configured, let's  configure it! 
     
Click "New SSH Key". A prompt will open up. On the right pane, you will see a command of "ssh-keygen" Follow the instructions given. 

![Authentication Options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vytk9n4bbj2qnnis0j1k.png)

![SSH Key Instructions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6zl66i5wd6omu3xgsmp.png)
    
Finally, click "Create Droplet"

![Click Create Droplet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fms6q8y1ggxtstu9yw0m.png)

3) Let's rename our droplet to something meaningful. I will be renaming mine to jenkins-tutorial-server. 

![Rename Droplet Name to Jenkins-Tutorial-Server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1uatwkc387nm5hjbm8i.png)

4) Copy the IPV4 address because we will need it when configuring our firewall rules.

5) Click on networking link and scroll down to Firewall and click "Edit"

![Click Networking Link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yskvb0wek8qymyporzax.png)

6) Next, we click the button that says "Create Firewall". 

7) We can name our firewall whatever we want. I will be naming mine "jenkins-tutorial-firewall". 

8) Next, we will paste our IPV4 address in the source of PORT 22. 

![Add Your IPV4 Address To Port 22 Source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cus2fu1r6875uwbutjbu.png)

9) We also want to add a custom port of 8080 to our Inbound Rules

![Add custom port of 8080 to inbound rules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8k0dtgza71lfjct68nf7.png)

10) Finally, add the droplet we created to the Firewalls Rules at the bottom and click "Create" 


### Accessing The Remote Server Via Terminal

Now, that we have created and configured our server on DigitalOcean, it is time to access it. 

1) Open the terminal and ssh into your server by using the IPv4 address.. 

![SSH into server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqqag30cqi9yvpzlvdnb.png)

2) Once you hit enter, you will be given a prompt of 

```
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

```

Type in "Yes" and hit enter again.

Now, you are inside your remote server. 

![Inside remote server via terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgetph4hpma91yb9uxjz.png)

3) Run `apt update`

![run command 'apt update'](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98mrsweicwg68iqilcli.png)

4) After that is complete, we need to install Docker by running the command run `apt install docker.io`

![Run command 'apt install docker.io'](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yipk4b4u0r9tm4oy0no1.png)

5) Then, to install Jenkins, we need to run the command `docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts`

With `p 8080:8080` we are binding the port that we specified in our Firewall of our server. This will allow us to access the UI of Jenkins. 

With `-p 50000:50000`, we are specifying the port that the controller and agent nodes speak to each other on. Jenkins runs on a Controller-Agent architecture (that's a lesson for another day).

With `-d`, we are running it in detached mode. This means that it will be running in the background. 

With `v jenkins_home:/var/jenkins_home` we are using Docker Volumes to reference a volume name to bind where the data is being stored to the local machine. This insures that when we stop our container, all the data and configurations we make is not lost. 

Finally, with `jenkins/jenkins:lts`, we are pulling and executing the official Jenkins latest image. 

6) After running this command, you want to type your IPv4 address, add a `:` and port `8080`. Once you do, you will be greeting with a Jenkins Page

![Unlock Jenkins Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kylzukkcthe8089v4bwh.png)

7) We need the initial password to login. To do this we need to do the following steps:

 - In the terminal run `docker ps`.
 - Copy the Container ID
 - Run `docker exec -it <container_image> /bin/bash`
 - Now, we are inside the docker container that is running 
   Jenkins.
 - Run `cat /var/jenkins_home/secrets/initialAdminPassword`
 - Copy the password and paste it into the field of the Sign   
   In Page. 

8) Next, after logging in successfully, we will select `Install Suggested Plugins` 

![Install Default Plugins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmc6hiff2y8j1kvwr4yb.png)

10) Now, we wait until all the plugins are installed

![Installing Plugins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlfs44ruuoe1s9w6ghwy.png)

 
11) Next, we create our own admin user. 

![Create Admin User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x36xctknmzckzlloxdzi.png)

12) Congratulations, you successfully setup Jenkins on your own remote server on DigitalOcean using Docker!!


Now, you can poke around, create jobs, install plugins, etc in Jenkins!


![Jenkins Home Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drcf2pyxxfa8eacscjzh.png)