Create Jenkins Server as Docker Container in Cloud

Create Jenkins Server as Docker Container in Cloud

In this post, we will create a Jenkins server as a Docker container in Digitalocean.

Create a Server in DigitalOcean

  • Environment: Ubuntu 22.10

  • Size: 4GB / 2CPUs

  • Open port 22 for SSH and 8080 for Jenkins

  • apt update

  • apt install net-tools

Install Docker && Jenkins

Run the commands to install Docker and start the Jenkins server

apt  install docker.io
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

We can run the following to validate the container running and inspect the docker_volume

root@jenkins-server:~# netstat lnpt | grep jenkins
tcp        0      0 jenkins-server:40090    172.67.148.71:http      TIME_WAIT
tcp        0      0 jenkins-server:40090    172.67.148.71:http      TIME_WAIT
tcp6       0    124 jenkins-server:ssh      114.226.77.36:19074     ESTABLISHED
tcp6       0      0 jenkins-server:ssh      114.226.77.36:21395     ESTABLISHED
root@jenkins-server:~# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
44485df1d7c8   jenkins/jenkins:lts   "/usr/bin/tini -- /u…"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   objective_leavitt
root@jenkins-server:~# docker volume inspect jenkins_home
[
    {
        "CreatedAt": "2022-12-04T09:29:39Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/jenkins_home/_data",
        "Name": "jenkins_home",
        "Options": null,
        "Scope": "local"
    }
]

Go into the Jenkins container and get the initial admin password

docker exec -it ${container_id} bash
cat /var/jenkins_home/secrets/initialAdminPassword

Then we open Jenkins with the server address and 8080 port. Follow the indication to create the first admin user. Then Jenkins is ready for use.

Install Build Tools in Jenkins

In Jenkins there are two ways to install the tools, one is through the UI and we can also install them in the server directly.

In this section, we will install Maven through Jenkins UI and npm&NodeJS with shell in the server.

Configure Plugin for Maven

Go to "Manage Jenkins->Global Tool Configuration" and add the Maven installations.

Installe npm and Node

After we login to the docker container, the user is jenkins by default which is a good practice. However, since we need to install applications, we need the admin user. Therefore, we use the flag "-u 0" for this.

docker exec -it -u 0 ${container_id} bash
cat /etc/issue
apt update
apt install nodejs npm -y # ver
node -v # validate the install

Make Docker available in the Jenkins container

Our Jenkins is installed as the Docker container. Inside Jenkins, we also need docker command. In this part, we will make Docker available inside the Jenkins container.

docker stop ${container_id}

docker run -p 8080:8080 -p 50000:50000 -d \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts

docker ps

docker exec -it -u 0 <container id> bash

curl https://get.docker.com > dockerinstall && chmod 777 dockerinstall && ./dockerinstall

chmod 666 /var/run/docker.sock

"-v jenkins_home:/var/jenkins_home" this is to map the existing jenkins directory "-v /var/run/docker.sock:/var/run/docker.sock" this is the Unix socket the Docker daemon listens on by default, and it can be used to communicate with the daemon from within a container.

After this, we can log in to the docker with Jenkins user and try to pull the Redis as the test.

docker exec -it ${container_id} bash
docker pull redis

Here is the result

jenkins@71449dc3b2f9:/$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a603fa5e3b41: Pull complete
77631c3ef092: Pull complete
ed3847cf62b8: Pull complete
261a8b530567: Pull complete
7d9005a8af6d: Pull complete
828da1afb5be: Pull complete
Digest: sha256:1e3207c292225b6dd21cb74d59255748a50e8f739dd983040df38fa913927cf1
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

Summary

In this post, we install Jenkins as a Docker container and we install the build tools with two different approaches: Jenkins standard configuration and shell command in the Jenkins server. Finally, we install the Docker inside the Jenkins.