In this project, we will be see how to use Git, Jenkins, Ansible, DockerHub, Docker to DEPLOY on a docker container.,
*Follow this project *
- Git
- Jenkins
- Ansible
- Setup ansible client and install docker.
- Docker Hub account
Put all the web page code file into github
- Create a Docker file into github
FROM centos:latest
MAINTAINER [email protected]
RUN yum install httpd git -y
RUN git clone https://github.com/praveensirvi1212/webdev /var/www/html
WORKDIR /var/www/html
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
EXPOSE 80
- Install java and git
- Install jenkins
- Login to Jenkins console
- Create Jenkins job with free style, Fill the following details,
-
Source Code Management:
- Repository :
https://github.com/praveensirvi1212/Docker.git
- Branches to build :
*/main
- Repository :
-
Build:
-
Send files or execute commands over SSH
- Name:
jenkins_server
- Exec Command:
rsync -avh /var/lib/jenkins/workspace/web/Dockerfile root@ansible-server's_private-ip:/opt
- Name:
-
Send files or execute commands over SSH
- Name:
ansible_server
- Exec Command:
cd /opt/docker
docker image build -t $JOB_NAME:v1.$BUILD_ID .
docker image tag $JOB_NAME:v1.$BUILD_ID praveensirvi/$JOB_NAME:v1.$BUILD_ID
docker image tag $JOB_NAME:v1.$BUILD_ID praveensirvi/$JOB_NAME:latest
docker image push praveensirvi/$JOB_NAME:v1.$BUILD_ID
docker image push praveensirvi/$JOB_NAME:latest
docker image rmi $JOB_NAME:v1.$BUILD_ID praveensirvi/$JOB_NAME:v1.$BUILD_ID praveensirvi/$JOB_NAME:latest
- Name:
-
-
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 id -
JOB_NAME
- Name of the project of this build. This is the name you gave your job when you first set it up.- Post Build:
- Send files or execute commands over SSH
- Name:
ansible_server
- Exec Command:
ansible-playbook /playbooks/docker.yml
- Name:
- Send files or execute commands over SSH
- Post Build:
- 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 job_name:v1.build_id
- 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
-
Install Ansible and Docker
-
Start Docker daemon
-
Login into Docker Hub
-
Write a yml file to create a container (file name : /playbooks/docker.yml)
--- - hosts: web tasks: - name: stop container shell: docker container stop myweb - name: remove container shell: docker container rm myweb - name: remove docker image shell: docker image rm praveensirvi/web - name: docker container create shell: docker run -d --name myweb -p 8080:80 praveensirvi/web
Troubleshooting: Makesure you have opened required ports on AWS Security group for this server.
- Install Docker and start Docker daemon