← All posts

Setting Up Jenkins CI in Docker

Illustration of running Jenkins in Docker

For better isolation and portability, I recommend running Jenkins in a container. The easiest way to do this is to use the ready-made docker-jenkinsci solution. Below, I’ll explain how to run it, configure Nginx, and migrate your data.

Installation

You can use my slightly modified fork of docker-jenkinsci, but it’s better to make your own with your own settings. Then clone it onto your server:

cd ~example
git clone https://github.com/adw0rd/docker-jenkinsci.git
cd docker-jenkinsci

Install Nginx, Docker, and Docker Compose as well:

sudo apt install nginx docker.io docker-compose

Configuring Nginx

Configure the TLS certificates and proxy requests to localhost:8080, or to the address specified in docker-compose.yml.

server {
    listen 443 ssl;
    server_name jenkins.example.org;
    ssl_certificate /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/privkey.pem;
    auth_basic "closed site";  # Рекомендую прятать Jenkins за base-auth, в Jenkins часто находят уязвимости, продукт популярный. Общая Basic Auth это дополнительная безопасность
    auth_basic_user_file htpasswd;  # Ниже показываю как сгенерировать этот файл, который Nginx будет искать в /etc/nginx

    location / {
        proxy_set_header Authorization "";  # Тут мы сбрасываем заголовок с Basic Auth, иначе Jenkins пытается его использовать для авторизации пользователя в своем интерфейсе. Я разделяю эти авторизации для себя
        proxy_pass http://localhost:8080;
    }
}

server {
    listen 80;
    server_name jenkins.example.org;
    return 301 https://$host$request_uri;
}

Read this article to learn how to get free SSL certificates from Let’s Encrypt.

To generate an htpasswd file, run:

echo "<USERNAME>:`openssl passwd -apr1`" > /etc/nginx/htpasswd

Replace <USERNAME> with your chosen username.

Installing Jenkins in Docker

Start Jenkins:

docker-compose up

During startup, a password will appear in the console. Copy it and enter it in the “Administrator password” field in the web interface (at https://jenkins.example.org ).

You’ll be asked which plugins to install. I recommend choosing “Select plugins to install” and selecting the smallest set that suits your needs (Git, Build Timeout, and any other checked plugins whose purpose you understand and which you know you need; you can install the rest later).

Next, create your administrator account in the “Create First Admin User” step.

Starting Jenkins Automatically in Docker

Now you can run Jenkins in the background:

docker-compose up -d

Or, better yet, place a systemd service file at /etc/systemd/system/docker-jenkinsci.service:

[Unit]
Description=Docker Compose Service for Jenkins CI
Requires=docker.service
After=docker.service

[Service]
WorkingDirectory=/home/example/docker-jenkinsci
ExecStart=/usr/bin/docker-compose up
ExecStop=/usr/bin/docker-compose down
TimeoutStartSec=0
Restart=on-failure

[Install]
WantedBy=multi-user.target

Now let’s enable and start our service:

sudo systemctl enable docker-jenkinsci sudo systemctl start docker-jenkinsci

You can also check the status, stop, or restart the service:

sudo systemctl status docker-jenkinsci sudo systemctl stop docker-jenkinsci sudo systemctl restart docker-jenkinsci

After changing the service file, reload systemd’s unit definitions:

systemctl daemon-reload

Migrating Jenkins Working Files

By default, the running container is named “docker-jenkinsci_master_1”, so that’s the name I’ll use below.

If you have the contents of a directory from a previous Jenkins installation (jobs, workspaces, configs), you can copy all that data into the volume:

cat var_lib_jenkins.tgz | docker exec -i docker-jenkinsci_master_1 tar Cxzf /var/jenkins_home/ -

After that, you can enter the container and check that you’ve put the data in the right place:

docker exec -uroot -ti docker-jenkinsci_master_1 bash
cd ~jenkins

Then restart Jenkins and check that everything works correctly through the web interface:

systemctl restart docker-jenkinsci

Backing Up Data

To copy the files from the container’s volume into a backup directory, use docker cp:

docker cp -a docker-jenkinsci_master_1:/var/jenkins_home backup_dir

Ask your questions in the comments.

Comments are closed. Read the original discussion in Russian →