Bachu ChennakesavaRao
Bachu ChennakesavaRao

Reputation: 29

How to customize the URL for Jenkins docker container?

I have installed jenkins docker in my system and am able to access the jenkins console with the local host url like http://localhost:8080.

Now, I wanted to share the URL with group of people. Some one suggest the steps to configure.

Upvotes: 3

Views: 3858

Answers (1)

Mohamed Benkedadra
Mohamed Benkedadra

Reputation: 2084

I am not sure of your Jenkins config as you haven't shared an MRE. So, this is how you launch a new Jenkins service that can be accessed by others on the network through Nginx.

We will be using Docker and docker-compose to facilitate the process. We're using the official Nginx and Jenkins images from docker hub.

  • Create a folder that contains the needed config files :
mkdir ~/jenkins-docker
cd ~/jenkins-docker
touch docker-compose.yml
touch nginx.conf
  • Make a home directory for Jenkins :
mkdir ~/jenkins
  • Create Jenkins and Nginx docker-compose services (docker-compose.yml file content):
version: '3'
services:
  jenkins:
    image: jenkins
    container_name: jenkins
    privileged: true
    user: root
    volumes:
      - ~/jenkins:/var/jenkins_home
    restart: always
    ports:
      - 8080:8080
    networks:
      - jenkinsnet

  server:
    image: nginx:1.17.2
    container_name: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf # your nginx config file
      - /var/log/nginx:/var/log/nginx # log files
    restart: always
    command: nginx-debug -g 'daemon off;'
    ports:
      - 8000:80
    networks:
      - jenkinsnet
    depends_on: 
      - jenkins

networks:
  jenkinsnet:
  • Create an Nginx config to make Jenkins accessible on the network (nginx.conf file content)
events {}

http {
    include mime.types;

    ## logging
    log_format  main  '$remote_addr - $remote_user [$time_local] [$server_name] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    # server config
    server {
        listen 80;

        location / {
            proxy_pass http://jenkins:8080;
        }
    }
}
  • Run your services :
cd ~/jenkins-docker
docker-compose up
  • Access Jenkins on your local machine on http://localhost:8080

  • Access Jenkins from other devices on your network on http://local-ip-address:8000 (ex: http://192.168.1.23:8000)

  • Access Jenkins from other devices connected to the internet http://public-ip-address:8000 (ex: http://56.137.222.112:8000) (Port Forwarding required if you're setting up on your home network. If you're using cloud providers, allow access to port 8000 for your instance)

Further Explanation

We are launching two docker containers. The jenkins container contains a Jenkins installation, accessible on port 8080 in the container. Consequently, we published that port in the jenkins service config, so that we can access it from the host machine using :

ports:
  8080:8080

The nginx container contains a reverse proxy server that allows you to make the Jenkins server accessible by routing all incoming traffic on a certain port to it.

In order for the nginx service to route traffic to the jenkins service, we create and assign a single network to the services:

# network creation :
networks:
  jenkinsnet:

# network assignement :
networks:
  - jenkinsnet

When the two containers belong to the same network, We are able to use the container names as hostnames. So accessing localhost:1234 on the jenkins container can be done from the nginx container using jenkins:1234. So, in the nginx.conf file we route all traffic coming to Nginx to the Jenkins server using :

location / {
    proxy_pass http://jenkins:8080;
}

Nginx is listening on port 80 :

server {
    listen 80;
...etc

So we publish the port to the host machine so that Nginx can pick up the incoming requests :

ports:
  - 8000:80

I chose port 8000 but you can use any port you like.

Upvotes: 3

Related Questions