In part-01 we
create Docker image on ansible server through Jenkins job and pushed it onto
DockerHub.
- Docker should be installed on ansible server
- Should login to "docker hub" on ansible
server
- Docker admin user should be part of docker group
In Part-02 we
create create_docker_container.yml playbook.
this get intiated by jenkins job, run by ansible and exected on dokcer_host
In Part-03 we
try to improvise to store docker images previous version
So for we used latest docker image to build a container, but
what happens if latest version is not working? One easiest solution is,
maintaining version for each build. This can be achieved by using environment
variables.
Take 3 EC2 REHL server as below for
Jenkins server, Ansible Server and Docker host
On Jenkins server:
- Do
the same setup for Jenkins server as project 1. (install jenkins, git,,
maven and java on REHL Jenkins server)
On Ansible Server install ansible as below:-
yum install ansible
ansible –version
adding client to ansible master
cd /etc/ansible
vi hosts
then add all clients private ip in this file on top and save
it
[web]
172.31.38.44 ...like this
then to check the master and client past below in master
[root@ip-172-31-18-161 ~]# ansible -m ping all
On Dokcer_host server:
- Do the same setup as project 3
To install Docker on RHEL server give the below command
docker --version
service
docker start
service docker status
usermod
-aG docker dockeradmin ----- Docker admin user
should be part of docker group
Note:
--Docker must be install in both Ansible Server(master) and Docker host Server(client)
also below command must be run in both the server(master and client)
[root@ip-172-31-89-34
docker]# docker login --username=rajguptaaws --password=aurangabad
Install below 2 plugging in Jenkins
publish over ssh and Deploy to container
then create a job in Jenkins and
give the below in different tab: -
Source
Code Management:
- Repository : https://github.com/rajkumargupta14/hello-world.git
- Branches to build : */master
Build:
- Root POM:pom.xml
- Goals and options : clean install package
Before doing below step create the entry for ansible_server in Manage Jenkins-
àConfigure System-
à Publish over
SSH-à then click on
ADD and fill the below
For more details check
the project 3
Note crate below in ansible server
[root@ip-172-31-40-233 ~]# mkdir /opt/docker
Post
Steps
- Send files or execute commands
over SSH
- Name: ansible_server
- Source files : webapp/target/*.war
- Remove prefix : webapp/target
- Remote directory : //opt//docker
- Send files or execute commands
over SSH
- Name: ansible_server
- Source files : Dockerfile
- Remote directory : //opt//docker
- Exec Command:
cd /opt/docker
docker build -t raj_demo4 .
docker tag raj_demo4 rajguptaaws/raj_demo4
docker push rajguptaaws/raj_demo4
docker rmi raj_demo4 rajguptaaws/raj_demo4
1.
Login to Docker host and check images and containers. (no images
and containers)
[dockeradmin@ip-172-31-40-233
~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[dockeradmin@ip-172-31-40-233
~]$ docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2.
login to docker hub and check. shouldn't find images with for raj_demo4
3.
Execute Jenkins job
4.
check images in Docker hub. Now you could able to see new images
pushed to rajguptaaws Docker_Hub
Note:- if
you get access issue then run the below command
[ec2-user@ip-172-31-35-164 ~]$ sudo -i
[root@ip-172-31-35-164 ~]# cd /opt/docker
[root@ip-172-31-35-164 docker]# chown -R dockeradmin:dockeradmin
/opt/docker
[root@ip-172-31-35-164
docker]#
Part-02 : Deploy Containers
In the
ansible server
[root@ip-172-31-35-164 ~]# cd /opt
[root@ip-172-31-35-164 opt]# mkdir playbooks
[root@ip-172-31-35-164 opt]# ls
containerd docker playbooks
[root@ip-172-31-35-164 opt]# cd playbooks/
[root@ip-172-31-35-164 playbooks]# pwd
/opt/playbooks
[root@ip-172-31-35-164 playbooks]# su - dockeradmin
Last login: Fri Feb 1
11:59:39 UTC 2019 on pts/1
[dockeradmin@ip-172-31-35-164 ~]$ cd /opt
[dockeradmin@ip-172-31-35-164 opt]$ ls
containerd docker playbooks
[dockeradmin@ip-172-31-35-164
opt]$ sudo chown dockeradmin: dockeradmin playbooks
[dockeradmin@ip-172-31-35-164
opt]$ cd playbooks/
[dockeradmin@ip-172-31-35-164
playbooks]$ sudo vi create_docker_container.yml
Then copy
the below code
- hosts: all
tasks:
- name: stop previous version docker
shell: docker stop raj_demo4
- name: remove stopped container
shell: docker rm -f raj_demo4
- name: remove docker images
shell: docker image rm -f rajguptaaws/raj_demo4
- name: create docker image
shell:
docker run -d --name raj_demo4 -p 8090:8080 rajguptaaws/raj_demo4
if you get the
permission issue then run the below command in ansible server
[dockeradmin@ip-172-31-35-164
opt]$ sudo chown dockeradmin: dockeradmin playbooks
[dockeradmin@ip-172-31-35-164
opt]$ cd playbooks/
[dockeradmin@ip-172-31-35-164
playbooks]$ ls -ld
drwxr-xr-x. 2
dockeradmin dockeradmin 78 Feb 1 12:37 .
to run playbook
manually use below command
[dockeradmin@ip-172-31-35-164
playbooks]$ ansible-playbook -v create_docker_container.yml
Now Add this script to Jenkins job.
- Chose "configure" to
modify your jenkins job.
- Under post build actions
- Send files or execute
commands over SSH
cd /opt/playbooks
ansible-playbook -v
create_docker_container.yml
1.
Execute Jenkins job.
2.
You could see a new container on your docker host. can able
access it from browser on port 8090
Part-03 : Deploy with Version Control Containers
we use 2 variables
- BUILD_ID -
The current build id of Jenkins (every time you click on build now it will
create new build id like 1,2,3,4,5,6……..)
- JOB_NAME -
Name of the project of this build. This is the name you gave your job when
you first set it up like in our case hellow-world
add the below part:-
Send files or execute commands over SSH
Name: ansible_server
Source files :
Dockerfile
Remote directory : //opt//docker
cd
/opt/docker
docker
build -t $JOB_NAME:v1.$BUILD_ID .
docker tag
$JOB_NAME:v1.$BUILD_ID rajguptaaws/$JOB_NAME:v1.$BUILD_ID
docker
tag $JOB_NAME:v1.$BUILD_ID rajguptaaws/$JOB_NAME:latest
docker
push rajguptaaws/$JOB_NAME:v1.$BUILD_ID
docker
push rajguptaaws/$JOB_NAME:latest
docker
rmi $JOB_NAME:v1.$BUILD_ID rajguptaaws/$JOB_NAME:v1.$BUILD_ID
rajguptaaws/$JOB_NAME:latest
Now do the changes in ansible code as per below yellow part:
- hosts: all
tasks:
- name: stop
previous version docker
shell: docker stop raj_demo4
- name: remove
stopped container
shell: docker rm -f raj_demo4
- name: remove
docker images
shell: docker
image rm -f rajguptaaws/hellow-world:latest
- name: create
docker image
shell: docker run
-d --name raj_demo4 -p 8090:8080 rajguptaaws/hellow-world:latest