In this example, we are going to let Jenkins build our Dockerised application after every git push process. My Jenkins server is based on Ubuntu 16.04 and the details are shown below as follows. I assume that you have the same setup and system is running.


Setup details


Server:
IP: 192.168.99.20
Jenkins GUI: http://192.168.99.20:8080/
Jenkins GUI login: admin:admin
Jenkisn Root: /var/lib/jenkins

Configuration:
Jenkins: version 2.89.2
Docker: version 17.09.1-ce, build 19e2cf6
Docker Compose: version 1.18.0, build 8dd22a9

Prerequisites


Allow jenkins user to run docker commands by running sudo usermod -aG docker jenkins command. After that you must restart Jenkins with sudo service jenkins restart command otherwise your builds will fail.


Structure


ubuntu@linux:~/hello-world$ tree -a
.
├── docker
│   ├── docker-compose.yml
│   └── .env
├── index.html
└── logs
└── nginx
├── access.log
└── error.log

3 directories, 6 files

Files


index.html


This is my web page!

.env


COMPOSE_PROJECT_NAME=helloworld

docker-compose.yml


version: '3'

services:
nginx_img:
container_name: ${COMPOSE_PROJECT_NAME}_nginx_con
image: nginx:1.13.8
volumes:
- ..:/usr/share/nginx/html
- ../logs/nginx:/var/log/nginx
ports:
- 1000:80
networks:
public_net:
ipv4_address: 192.168.0.11

networks:
public_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.0.0/24

If you want to know, Nginx file paths are shown below as follows.


Config file: /etc/nginx/nginx.conf
Vhost file: /etc/nginx/conf.d/default.conf

Build


ubuntu@linux:~/hello-world/docker$ docker-compose up -d --build

Test to see if Nginx container is accessible.


ubuntu@linux:~$ curl 192.168.0.11
This is my web page!

ubuntu@linux:~$ curl localhost:1000
This is my web page!

Jenkins setup


GitHub SSH connection


ubuntu@linux:~$ sudo su -l jenkins
jenkins@linux:~$

jenkins@linux:~$ ssh-keygen -t rsa -b 4096 -C "email@address.com"
jenkins@linux:~$ eval "$(ssh-agent -s)"
jenkins@linux:~$ ssh-add ~/.ssh/id_rsa
jenkins@linux:~$ cat ~/.ssh/id_rsa.pub # Add this to GitHub
jenkins@linux:~$ ssh -T git@github.com

Jenkins and GitHub integration


I have blog posts already written about it under "Jenkins" so you better read and apply them yourself. I won't add them here because the process is long to explain.


Add build command to run


Login to Jenkins GUI and the following.



Test


If you click "Build now", your build should pass with results below.


Started by user admin
Building in workspace /var/lib/jenkins/workspace/Hello-World
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/inanzzz/helloworld.git # timeout=10
Fetching upstream changes from https://github.com/inanzzz/helloworld.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/inanzzz/helloworld.git +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
> git show-ref --tags -d # timeout=10
Checking out Revision 11c315d020cf6b1791b3294d381357dfc3ce9620 (origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 11c315d020cf6b1791b3294d381357dfc3ce9620
Commit message: "Bring back volumes"
> git rev-list --no-walk 11c315d020cf6b1791b3294d381357dfc3ce9620 # timeout=10
[Hello-World] $ /bin/sh -xe /tmp/jenkins8895909563541010771.sh
+ cd docker
+ docker-compose up -d
helloworld_nginx_con is up-to-date
+ curl localhost:1000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 21 100 21 0 0 3718 0 --:--:-- --:--:-- --:--:-- 4200
This is my web page!
Finished: SUCCESS

Server structure after the build


ubuntu@linux:~$ sudo su -l jenkins

jenkins@linux:~/workspace/Hello-World$ ls -l
total 12
drwxr-xr-x 2 jenkins jenkins 4096 Dec 24 23:22 docker
-rw-r--r-- 1 jenkins jenkins 21 Dec 24 20:47 index.html
drwxr-xr-x 3 root root 4096 Dec 24 21:02 logs

jenkins@linux:~/workspace/Hello-World$ ls -l logs/
total 4
drwxr-xr-x 2 root root 4096 Dec 24 21:02 nginx

jenkins@linux:~/workspace/Hello-World$ ls -l logs/nginx/
total 4
-rw-r--r-- 1 root root 455 Dec 24 23:22 access.log
-rw-r--r-- 1 root root 0 Dec 24 21:02 error.log

Docker image


jenkins@linux:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.13.8 3f8a4339aadd Less than a second ago 108MB
...

Docker container


jenkins@linux:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
041fde7a1975 nginx:1.13.8 "nginx -g 'daemon ..." 8 minutes ago Up 8 minutes 0.0.0.0:1000->80/tcp helloworld_nginx_con

Docker network


jenkins@linux:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9f0596a9b6dc helloworld_public_net bridge local