Jenkins, a widely used open-source application developed in Java, is ideal for creating continuous integration and continuous deployment (CI/CD) pipelines, accelerating the delivery of software. It plays a central role in automating the Software Development Life Cycle (SDLC). This tutorial outlines each step for deploying Jenkins on a instance running Ubuntu, with the help of Docker Compose.
System Requirements
- A newly deployed cloud instance with Ubuntu 20.04 or 18.04
- Docker and Docker Compose must be pre-installed on the system
Matching infrastructure at centron
From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →
Creating a Docker Compose File for Jenkins
Start by creating a directory named jenkins-compose and placing a file named docker-compose.yml inside it.
$ mkdir jenkins-compose
$ cd jenkins-compose
$ touch docker-compose.ymlNow, launch your favorite text editor and open the new file:
$ nano docker-compose.ymlInsert the script below into the docker-compose.yml file:
version: '3.8'
services:
jenkins:
container_name: jenkins
restart: always
image: jenkins/jenkins:lts
ports:
- 8080:8080
volumes:
- jenkins-home:/var/jenkins_home
volumes:
jenkins-home:Understanding the Compose File Structure
version: '3.8'
Specifies the format version for the Docker Compose file. Using a different version may cause compatibility issues with this configuration.
services:
Defines the containers that will be launched. Each container configuration is grouped under this section.
jenkins:
container_name: jenkins
restart: always
image: jenkins/jenkins:lts
ports:
- 8080:8080
volumes:
- jenkins-home:/var/jenkins_homeThis configuration section specifies key details for the Jenkins container, including its name, the restart behavior, the image version from Docker Hub, and the port/volume mappings.
The restart: always instruction ensures that the Jenkins container automatically restarts after a server reboot.
volumes:
Defines a volume named jenkins-home that will be used by the Jenkins container to store persistent data.
Starting the Docker Service
Once the Compose file is ready, activate and launch the Docker service. Enabling the service ensures that Docker will start automatically when the system reboots.
$ sudo systemctl enable docker.service
$ sudo systemctl start docker.serviceDeploying the Jenkins Container
With the Docker Compose file in place, you can now run Jenkins in detached mode using the following command:
$ docker-compose up -dThis command will fetch the official Jenkins image from Docker Hub and start the container in the background.
Verifying the Container Status
To confirm that Jenkins is running successfully, execute the following:
$ docker-compose psEnable SSL Certificates for Jenkins with Nginx
To make Jenkins accessible via a secure HTTPS connection, you’ll need to set up the Nginx web server, configure SSL certificates, and forward incoming traffic to the Jenkins backend.
Install the Nginx Web Server
# apt install nginx -yConfigure Nginx for Jenkins
Edit the default configuration file for Nginx as follows:
# nano /etc/nginx/conf.d/default.confInsert the domain configuration block:
server {
server_name example.com www.example.com
}To enable SSL, follow a Let's Encrypt tutorial tailored for Nginx. The process updates the same configuration file automatically with SSL directives. Open it again to continue editing:
# nano /etc/nginx/conf.d/default.confAdd these proxy instructions to route traffic to Jenkins:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}Example Final Nginx Configuration
The resulting Nginx configuration will appear like this:
server {
server_name example.com www.example.com;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com www.example.com;
listen 80;
return 404; # managed by Certbot
}Firewall Configuration for a Server
To enhance security on your machine, configure the firewall to permit only essential traffic—ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
Set Up the Operating System Firewall (UFW)
Install the Uncomplicated Firewall (UFW):
# apt install ufwBlock all incoming traffic and allow outgoing traffic by default:
# ufw default deny incoming
# ufw default allow outgoingThen allow specific ports:
# ufw allow ssh
# ufw allow http
# ufw allow httpsImportant: Before enabling the firewall, ensure that the SSH port remains open to avoid losing remote access to the server.
Finally, activate the firewall and verify its status:
# ufw enable
# ufw statusVerify Jenkins Server Setup
Once Jenkins is up and running, navigate to https://www.example.com/ in your browser. The system will request the initial Administrator password.
Execute the following command to retrieve the password from the Jenkins container:
$ docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPasswordJenkins will prompt you to install recommended plugins. Proceed with the installation. Then, create an admin user by entering the requested information. After that, your Jenkins instance is ready for creating CI/CD pipelines.
Final Thoughts
Docker Compose is a convenient utility for managing and deploying container-based applications. In this guide, we demonstrated how to install and run Jenkins on a Ubuntu server using Docker Compose, configure HTTPS with Nginx, and secure access via firewalls.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.