How to run Jenkins in docker container on Ubuntu 18.04

How to run Jenkins in docker container on Ubuntu 18.04

Overview

  1. OS Ubuntu 18.04
  2. Docker needs to be installed on the system

Run Jenkins as docker container:

  1. Install docker on the system:
    sudo apt install -y docker.io
    
  2. Add your current user to the docker group:
    sudo usermod -aG docker [username]
    
  3. Exit and log back into the server and check the current user's groups:
    groups
    
  4. Create Jenkins docker network:
    docker network create jenkins
    
  5. Create docker volume for Jenkins certs:
    docker volume create jenkins-docker-certs
    
  6. Create docker volume for Jenkins data:
    docker volume create jenkins-data
    
  7. Run docker container that manages certs:
    docker container run --name jenkins-docker --rm --detach \
    --privileged --network jenkins --network-alias docker \
    --env DOCKER_TLS_CERTDIR=/certs \
    --volume jenkins-docker-certs:/certs/client \
    --volume jenkins-data:/var/jenkins_home \
    --publish 2376:2376 docker:dind
    
  8. Run docker container with Blue Ocean plugin:
    docker container run --name jenkins-blueocean --rm --detach \
    --network jenkins --env DOCKER_HOST=tcp://docker:2376 \
    --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
    --volume jenkins-data:/var/jenkins_home \
    --volume jenkins-docker-certs:/certs/client:ro \
    --publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean
    
  9. Check if containers are up and running:
    docker ps
    
  10. Verify that Jenkins dashboard is up and running. Go to the URL address of the Jenkins server [jenkins_server_url]:8080: jenkins
  11. To unlock the Jenkins, get the password from UI (Blue Ocean) container:
    docker logs jenkins-blueocean