Description: Here I have explained, How to setup pipeline to deploy Java Project in Tomcat docker container using Jenkins.
Setup:
- Jenkins Server [localhost]
- Docker host [52.12.158.81]
Jenkins Plugin:
- Publish Over SSH
First I am going to create user for authentication in Docker host and create separate directory to save project ".war" file. Also add that user in docker group, so execute docker command as well.
Create user with home directory and set password
# useradd dockeradmin -d /home/dockeradmin
# useradd dockeradmin -d /home/dockeradmin
# passwd dockeradmin
Here I have use password not key for authentication, so we need to change in sshd configuration file to allow password authentication.
# vi /etc/ssh/sshd_config
PasswordAuthentication yes ### set yes to allow user authentication password
After changes in configuration file restart ssh service
Add dockeradmin user to docker group using below command
# usermod -aG docker dockeradmin
Configure user and docker host in Jenkins
Open the Jenkins url and navigate to Manage Jenkins --> Configure System --> Publish Over SSH [This option shows only if publish over ssh plugin installed] --> SSH servers [fill all the require details as follow]
Name: docker
Host name: 52.12.158.81 [IP address of Docker Host]
Username: dockeradmin
Remote Directory: /home/dockeradmin [user's Home Directory]
Password: [Set user password]
Port: 22 [change if custom set]
After save all the details, click on test connection, it shows success if all things perfect.
Create custom Dockerfile for tomcat container
In tomcat container with latest release, when we pull the image and deploy it directly. It showing an 404 error, so to overcome this issue we need to rename webapps.dist to webapps
First I am creating one folder under /opt with name docker and navigate to that folder
# mkdir /opt/docker
# cd /opt/docker
Create Dockerfile for pull latest tomcat image, then copy webapps.dist to webapps
# vi Dockerfile
FROM tomcat:latest RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps COPY ./*.war /usr/local/tomcat/webapps
After save Dockerfile, give permission to dockeradmin user on created folder [/opt/docker] using below command
# chown -R dockeradmin.dockeradmin /opt/docker
Create Maven Project on Jenkins: After setup all the things on docker machine, now I am going to create Maven Project on Jenksins.
To setup project navigate to Jenkins Dashboard --> New Item
Fill all required details like Name and select Maven Project
After click on OK, fill the details for the projects like git repo, build trigger, Build Options, SSH details of server etc..
Git Repo:
Git Repo:
Build Triggers: In poll SCM, I have set * * * * *, means whenever change occurred on git repository pipeline will executed.
Build:
Post Steps:
Select docker machine, which we have added before in copy over SSH
Fill the details for transfer files, remove prefix, Remote Directory [where you want to copy war file after build, So in our case I will copy to /opt/docker directory] which is owned by dockeradmin user
Source Files : **/*.war [Jave project build save in war extention]
Remove Prefix : target [It will remove all the folder prefix, we set this to avoid file path confusion]
Remote Directory : //opt//docker [We must use // double slash]
Exec Command: Below command will run while pipeline executed.
cd /opt/docker; [ It will change the Directory to project path]
docker build -t tomcat:v1 .; [Build the image using Dockerfile, which we have defined before]
docker container stop jenkins; [This command stopped the existing container]
docker container rm jenkins; [This command remove the container]
docker run -d --name jenkins -p 8080:8080 tomcat:v1
[This command will create container using the custom image created from dockerfile]
[This command will create container using the custom image created from dockerfile]
After filled all the above details, click on apply and save. Build the project or change in github repository to automated execute the pipeline.
So for the test, I am change in index.jsp file as follow and commit the changes
So for the test, I am change in index.jsp file as follow and commit the changes
After changes, we can see build automatically trigger
Also verify the URL http://52.12.158.81:8080/SimpleTomcatWebApp
Any changes occurred on git hub code, automatically trigger the job and it overwrite the container.
No comments:
Post a Comment