Ansible
In this section we will create Automate creating Docker images and containers but executing commands directly from Jenkins rather than logging into shell. Playbooks to automate most of the task.
Last updated
In this section we will create Automate creating Docker images and containers but executing commands directly from Jenkins rather than logging into shell. Playbooks to automate most of the task.
Last updated
Before we proceed with creating our Ansible Playbook let us delete the running container and images we created earlier manually because we are going to do it automatically using Ansible.
Lets list all the container : docker ps -a
To delete any running container stop the container by running: docker stop <containerID>
To delete any container: docker rm <containerID>
To delete any images created by you or dockerfile: docker rmi <imageID>
Now everything is fresh we have no running containers. In previous section we ran commands like docker run to run the container and docker cp to copy the war file in webapps folder but now we will order Jenkins to automate it!
Login to JENKINS >> goto your Job>> Configure >> Post Build actions >> In the publish over SSH section fill in these commands in "Exec Commands" column:
Let's Try Build Now and if you get this Everything is working fine. Now Jenkins should have created image and run the container
You can even visit your website by typinf <IP>:8080/webapp/
How AMAZING! We have tomcat image and container running with build file copied in webapp folder just in 1 Click!!!!!
BUT, BUT there is still a major problem in this infrastructure according to the commands JENKINS will create image named as tomcatimage and container named tomcatcontainer. So if we try to run build again it will show error in console output as the container with that name already exist. UBT
To solve this problem we will be writing ANSIBLE playbook which will delete any existing container and then launch a new one. So for all the interesting knowledge on ANSIBLE jump to next section.