PoshJosh's Blog

Installing and running Jenkins in Docker

February 14, 2019

Background

Jenkins can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software. Read a brief Introduction to Jenkins, if you haven’t done that already.

Downloading and Running Jenkins in Docker

  • Create a bridge network in Docker.

Open command prompt and type the following docker network create command:

C:\WINDOWS\system32>docker network create jenkins
22adcb99b9197d6ebdec8b601bb165e8dAycum786c29091ee612c1489f2lo09w
  • Create volume for Docker client TLS certificates.

The volume will be used to share the Docker client TLS certificates needed to connect to the Docker daemon:

C:\WINDOWS\system32>docker volume create jenkins-docker-certs
jenkins-docker-certs
  • Create volume for persisting jenkins data.

Use the following command:

C:\WINDOWS\system32>docker volume create jenkins-data
jenkins-data
  • Download and run docker-in-docker image.

In order to execute Docker commands inside Jenkins nodes, download and run the docker:dind Docker image using the following docker container run command:

C:\WINDOWS\system32>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 docker:dind

unable to find image 'docker:dind' locally
dind: Pulling from library/docker
c9b1b535fdd9: Pull complete
.
.                                                                                                          sha256:a4f33d003b7ec9133c2a1ff61f4e80305b329c0fa8b753140b9ab2808f28328c
Status: Downloaded newer image for docker:dind
22adcb99b9197d6ebdec8b601bb165e8dAycum786c29091ee612c1489f2lo09w

-Download and run the jenkinsci/blueocean image.

I changed my port binding from 8080:8080 to 8090:8090. This result in some problems in initial startup via the web browser in windows 10. So I had to revert back to 8080.

C:\WINDOWS\system32>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

Unable to find image 'jenkinsci/blueocean:latest' locally
latest: Pulling from jenkinsci/blueocean
e7c96db7181b: Already exists
f910a506b6cb: Already exists
.
.
f58b5d821a07: Pull complete
Digest: sha256:bbo8uwhadcc99b9197d6ebdec8b601bb165e8dAycum786c29091ee612c1489f2lo09w
Status: Downloaded newer image for jenkinsci/blueocean:latest
cef7d6ebdec8b601bb165e8dAycum786c29091ee612c1489f2lo09w
  • Access jenkins

Browse to http://localhost:8080

  • Access the initial admin password.

To access the required initial password, run following command.

docker container logs jenkins-blueocean

Some where in the logs you will find the password between some lines of asterix as shown:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

2c4b5c8dBbAx01a7a8dc1p0o33337e

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

After entering the password you will be prompted to install plugins.

Save this password - you may need it if one or more errors occur during installation

If the process of installing plugins returns the following error:

An error occurred during installation no such plugin cloudbees-folder

Then stop the docker jenkins-blueocean container and re-run it, but this time without mapping the jenkins-data as we did previously via the following expression:

--volume jenkins-data:/var/jenkins_home

Here is the stop command followed by the run command

C:\WINDOWS\system32>docker container stop jenkins-blueocean
C:\WINDOWS\system32>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-docker-certs:/certs/client:ro --publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean
  • Access jenkins

Browse to http://localhost:8080

Use command docker container logs jenkins-blueocean to view the password as done earlier.

Save this password

After successfully browsing to jenkins.. you have to re-run the container again with the mapping of the jenkins-data.

C:\WINDOWS\system32>docker container stop jenkins-blueocean
C:\WINDOWS\system32>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

Enter the first password you saved.

The plugins should install successfully now.

You no longer need to keep the saved passwords

Note: Info by Jenkins

You have skipped the setup of an admin user.

To log in, use the username: "admin" and the administrator password you used to access the setup wizard.

If you use docker in windows via Docker ToolBox and browser complains that it can’t reach the server

Don’t check http://localhost:8080 in your browser. This is because, that is not the actual host. The actual host is running in Vmware and the IP address of the actual host could be viewed by running the following command as root user:

C:\WINDOWS\system32>docker-machine ip default
192.168.99.100

Now access jenkins via 192.168.99.100:8080

List machines

C:\WINDOWS\system32>docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.7

List processes

C:\WINDOWS\system32>docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
52c60eacd03d        jenkinsci/blueocean   "/sbin/tini -- /usr/…"   About an hour ago   Up About an hour    0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins-blueocean
  • SSH into a Container

    • Use docker ps to get the name of the existing container.
    • Use the command docker exec -it /bin/bash to get a bash shell in the container.
    • Generically, use docker exec -it to execute whatever command you specify in the container.
docker exec -u 0 -it <container_name_or_id> /bin/bash

Here, the -u 0 flag specifies that the root user with id 0 be used to run /bin/bash

When we ran a Dockerfile which attempted to download maven:3-alpine, I encountered the following exception:

C:\WINDOWS\system32>docker pull maven:3-alpine
error during connect: Post https://docker:2376/v1.39/images/create?fromImage=maven&tag=3-alpine: dial tcp: lookup docker on 127.0.0.11:53: no such host
script returned exit code 1

The problem was the dns of 127.0.0.11. So change it via the following run command, with google public dns of 8.8.8.8 incorporated:

C:\WINDOWS\system32>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 --dns 8.8.8.8 --dns 127.0.0.11 jenkinsci/blueocean

I got a warning this time, as follows:

WARNING: Localhost DNS setting (--dns=127.0.0.11) may fail in containers.
8de9587bb1c053a63175661608753d581306d5ac53a192069eb6738cd9636c8f

So we stop the container and re-run with 8.8.8.8 and 8.8.4.4, both google public dns:

C:\WINDOWS\system32>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 --dns 8.8.8.8 --dns 8.8.4.4 jenkinsci/blueocean

References


Written byChinomso IkwuagwuExcélsior

Limited conversations with distributed systems.

Modifying legacy applications using domain driven design (DDD)

Gherkin Best Practices

Code Review Best Practices

Hacking Cypress in 9 minutes

Some common mistakes when developing java web applications

How to make a Spring Boot application production ready

SQL JOINS - A Refresher

Add Elasticsearch to Spring Boot Application

Add entities/tables to an existing Jhipster based project

CSS 3 Media Queries - All over again

Maven Dependency Convergence - quick reference

Amazon SNS Quick Reference

AWS API Gateway Quick Reference

Amazon SQS Quick Reference

AWS API Gateway Quick Reference

AWS Lambda Quick Reference

Amazon DynamoDB - Quick Reference

Amazon Aurora

Amazon Relational Database Service

AWS Database Services

AWS Security Essentials

Amazon Virtual Private Cloud Connectivity Options

Summary of AWS Services

AWS Certified Solutions Architect - Quick Reference

AWS CloudFront FAQs - Curated

AWS VPC FAQs - Curated

AWS EC2 FAQs - Curated

AWS Achritect 5 - Architecting for Cost Optimization

AWS Achritect 4 - Architecting for Performance Efficiency

AWS Achritect - 6 - Passing the Certification Exam

AWS Achitect 3 - Architecting for Operational Excellence

AWS Achitect 2 - Architecting for Security

AWS Achitect 1 - Architecting for Reliability

Amazon DynamoDB Accelerator (DAX)

Questions and Answers - AWS Certified Cloud Architect Associate

Questions and Answers - AWS Certified Cloud Architect Associate

AWS Connectivity - PrivateLink, VPC-Peering, Transit-gateway and Direct-connect

AWS - VPC peering vs PrivateLink

Designing Low Latency Systems

AWS EFS vs FSx

AWS Regions, Availability Zones and Local Zones

AWS VPC Endpoints and VPC Endpoint Services (AWS Private Link)

AWS - IP Addresses

AWS Elastic Network Interfaces

AWS Titbits

Jenkins on AWS - Automation

Jenkins on AWS - Setup

Jenkins on AWS - Best practices

Introduction to CIDR Blocks

AWS Lamda - Limitations and Use Cases

AWS Certified Solutions Architect Associate - Part 10 - Services and design scenarios

AWS Certified Solutions Architect Associate - Part 9 - Databases

AWS Certified Solutions Architect Associate - Part - 8 Application deployment

AWS Certified Solutions Architect Associate - Part 7 - Autoscaling and virtual network services

AWS Certified Solutions Architect Associate - Part 6 - Identity and access management

AWS Certified Solutions Architect Associate - Part 5 - Compute services design

AWS Certified Solutions Architect Associate - Part 4 - Virtual Private Cloud

AWS Certified Solutions Architect Associate - Part 3 - Storage services

AWS Certified Solutions Architect Associate - Part 2 - Introduction to Security

AWS Certified Solutions Architect Associate - Part 1 - Key services relating to the Exam

AWS Certifications - Part 1 - Certified solutions architect associate

AWS Virtual Private Cloud (VPC) Examples

Curated info on AWS Virtual Private Cloud (VPC)

Notes on Amazon Web Services 8 - Command Line Interface (CLI)

Notes on Amazon Web Services 7 - Elastic Beanstalk

Notes on Amazon Web Services 6 - Developer, Media, Migration, Productivity, IoT and Gaming

Notes on Amazon Web Services 5 - Security, Identity and Compliance

Notes on Amazon Web Services 4 - Analytics and Machine Learning

Notes on Amazon Web Services 3 - Managment Tools, App Integration and Customer Engagement

Notes on Amazon Web Services 2 - Storages databases compute and content delivery

Notes on Amazon Web Services 1 - Introduction

AWS Auto Scaling - All you need to know

AWS Load Balancers - How they work and differences between them

AWS EC2 Instance Types - Curated

Amazon Web Services - Identity and Access Management Primer

Amazon Web Services - Create IAM User

Preparing Jenkins after Installation

Jenkins titbits, and then some

Docker Titbits

How to Add Chat Functionality to a Maven Java Web App

Packer - an introduction

Terraform - an introduction

Versioning REST Resources with Spring Data REST

Installing and running Jenkins in Docker

Automate deployment of Jenkins to AWS - Part 2 - Full automation - Single EC2 instance

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

Introduction to Jenkins

Software Engineers Reference - Dictionary, Encyclopedia or Wiki - For Software Engineers