CI: How to create a Nginx reverse proxy for Jenkins in arm64 macOS using Docker Compose?
My struggles resolving HTTP 502 bad gateway when creating Nginx reverse proxy for Jenkins in following the reverse proxy configuration — Nginx in Jenkins website. I created this post hoping to help others who are in the same struggle. Although their is a video from CloudBeesTV which was helpful for me to understand how Nginx reverse proxy configuration needs to be changed but as the presenter in the video performed a command inside the docker container to resolve the HTTP 502 bad gateway and in my case it was not working as the presenter and I were using a different distribution of docker build image of Nginx.
If you like my articles, please follow me on Medium and you can also support me by buying me a coffee.
Docker Compose
Create `docker-compose.yml` file and paste the following code,
version: '3.7'
services:
jenkins:
image: jenkins/jenkins:lts
user: root
ports:
- "50000:50000"
volumes:
- 'jenkins-data:/var/jenkins_home'
- '/var/run/docker.sock:/var/run/docker.sock'
- '$HOME:/home'
networks:
- jenkins-net
environment:
- JAVA_OPTS=-Djenkins.install.runSetupWizard=false
nginx:
image: nginx:stable
user: root
networks:
- jenkins-net
ports:
- '80:80'
volumes:
- '/var/log/nginx/jenkins'
- '$PWD/jenkins.conf:/etc/nginx/conf.d/default.conf'
- '/var/run/docker.sock:/var/run/docker.sock'
volumes:
jenkins-data:
networks:
jenkins-net:
In the above code,
I’m using the jenkins lts docker build image
image: jenkins/jenkins:lts
as a root user
user: root
Inbound Jenkins agents communicate with the Jenkins controller through TCP port 50000 by default.
ports:
- "50000:50000"
I’m using jenkins-data
volume is used and /var/run/docker.sock
is basically the Unix socket the Docker daemon listens on by default. It is also a tool used to communicate with the Docker daemon from within a container. Sometimes, containers need to bind mount the /var/run/docker.sock
file.
volumes:
- 'jenkins-data:/var/jenkins_home'
- '/var/run/docker.sock:/var/run/docker.sock'
- '$HOME:/home'
using the internal network jenkins-net
networks:
- jenkins-net
Skipping the default setup wizard in jenkins
environment:
- JAVA_OPTS=-Djenkins.install.runSetupWizard=false
I’m using nginx stable docker build image
image: nginx:stable
The localhost 127.0.0.1
is listening to port 80
ports:
- '80:80'
A new file is created in Nginx docker container for storing jenkins logs. The local file jenkins.conf
replaces the default.conf
in the Nginx docker container.
volumes:
- '/var/log/nginx/jenkins'
- '$PWD/jenkins.conf:/etc/nginx/conf.d/default.conf'
- '/var/run/docker.sock:/var/run/docker.sock'
The networks and volumes are created
volumes:
jenkins-data:
networks:
jenkins-net:
Docker network: jenkins-net
Docker network needs to be created in order to interconnect Nginx and Jenkins docker containers.
docker network create — driver bridge jenkins-net
Nginx Jenkins configuration
Nginx jenkins configuraiton file that is used by the Nginx proxy server to forward requests to Jenkins.
Create a file jenkins.conf
and paste the following code,
# Required for Jenkins websocket agents
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name jenkins.example.com
access_log off;
# this is the jenkins web root directory
# (mentioned in the output of "systemctl cat jenkins")
root /var/run/jenkins/war/;
access_log /var/log/nginx/jenkins.access.log;
error_log /var/log/nginx/jenkins.error.log;
# pass through headers from Jenkins that Nginx considers invalid
ignore_invalid_headers off;
location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
# rewrite all static files into requests to the root
# E.g /static/12345678/css/something.css will become /css/something.css
rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}
location / {
proxy_pass http://jenkins:8080;
proxy_redirect default;
proxy_http_version 1.1;
# Required for Jenkins websocket agents
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_max_temp_file_size 0;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffer_size 8k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
Run
In terminal of macOS or in the app Visual Studio Code, go to the file directory where the above two files are saved. Run the following command,
docker compose up -d
Now docker will create a docker compose container that has both jenkins and nginx containers
You also need to make a change in the file /etc/hosts
and add
127.0.0.1 jenkins.example.com
to /etc/hosts
file.
Open the URL `http://jenkins.example.com/` in a browser and you will see the Jenkins dashboard.
Here is the GitHub repository: https://github.com/akarsh/jenkins-nginx-reverse-proxy-configuration
References