In this project, we will be see how to use Git, Jenkins, Ansible, DockerHub, Docker to DEPLOY on docker containers.,
Follow this project in Youtube
- Jenkins - Get help here
- Ansible - Get help here
- Setup a EC2 instahce as ansible client and install docker. here
In part-01 we create Docker image on ansible server through Jenkins job and pushed it onto DockerHub.
-
Login to Jenkins console
-
Create Jenkins job, Fill the following details,
- Source Code Management:
- Repository :
https://github.com/ValaxyTech/hello-world.git
- Branches to build :
*/master
- Repository :
- Build:
- Root POM:
pom.xml
- Goals and options :
clean install package
- Root POM:
- 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
- Name:
-
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 valaxy_demo:v1.0 .
- Name:
-
Send files or execute commands over SSH
- Name:
ansible_server
- Source files:
Dockerfile
- Remote directory:
//opt//docker
- Exec Command:
docker tag valaxy_demo:v1.0 valaxy/valaxy_demo:v1.0 docker push valaxy/valaxy_demo:v1.0 docker rmi valaxy_demo:v1.0 valaxy/valaxy_demo:v1.0 ```
- Name:
-
- Source Code Management:
-
Login to Docker host and check images and containers. (no images and containers)
-
login to docker hub and check. shouldn't find images with for valaxy_demo
-
Execute Jenkins job
-
check images in Docker hub. Now you could able to see new images pushed to Valaxy Docker_Hub
- 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
-
Write a yml file to create a container (file name : create_docker_container.yml)
--- - hosts: web-servers become: true tasks: - name: stop previous version docker shell: docker stop valaxy_demo - name: remove stopped container shell: docker rm -f valaxy_demo - name: create docker image shell: docker run -d --name valaxy_demo -p 8090:8080 valaxy/valaxy_demo
-
Add this script to Jenkins job.
- Chose "configure" to modify your jenkins job.
- Under post build actions
- Send files or execute commands over SSH
- Exec Command:
cd /opt/playbooks ansible-playbook create_docker_container.yml
- Send files or execute commands over SSH
- Under post build actions
- Chose "configure" to modify your jenkins job.
-
Execute Jenkins job.
-
You could see a new container on your docker host. can able access it from browser on port 8090
Troubleshooting: Makesure you have opened required ports on AWS Security group for this server.
In Part-03 we try to improvise to store docker images previous versions
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.
here we use 2 variables
BUILD_ID
- The current build idJOB_NAME
- Name of the project of this build. This is the name you gave your job when you first set it up.
for more info Please refer this URL
Lets modify jenkins job which was created in Part-01 as below.
- Create Jenkins job
-
Source Code Management:
- Repository :
https://github.com/ValaxyTech/hello-world.git
- Branches to build :
*/master
- Repository :
-
Build:
- Root POM:
pom.xml
- Goals and options :
clean install package
- Root POM:
-
Send files or execute commands over SSH
- Name:
ansible_server
- Source files :
webapp/target/*.war
- Remove prefix :
webapp/target
- Remote directory :
//opt//docker
- Name:
-
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 $JOB_NAME:v1.0.$BUILD_ID .
- Name:
-
Send files or execute commands over SSH
- Name:
ansible_server
- Source files:
Dockerfile
- Remote directory:
//opt//docker
- Exec Command:
docker tag $JOB_NAME:v1.0.$BUILD_ID valaxy/$JOB_NAME:v1.0.$BUILD_ID docker tag $JOB_NAME:v1.0.$BUILD_ID valaxy/$JOB_NAME:latest docker push valaxy/$JOB_NAME:v1.0.$BUILD_ID docker push valaxy/$JOB_NAME:latest docker rmi $JOB_NAME:v1.0.$BUILD_ID valaxy/$JOB_NAME:v1.0.$BUILD_ID valaxy/$JOB_NAME:latest
- Name:
-