Table of contents
EXAMPLE - 1
Create a new Jenkins freestyle project named "Django-todo-cicd"
Configure the project. In the Source Code Management section > Select Git > Enter the GitHub Repository Link and the branch name.
Repo link I have used: Django-todo-cicd
In the Build Steps section > Select Add Build Steps > Select Execute Shell > And type the commands required for your activity.
docker build -t todoapp . docker run -d --name django-todo-app -p 8000:8000 todoapp:latest
Click on Save and Build your project.
Once the project is built successfully, a green tick can be observed beside the build number.
This #1 failed due to docker not installed in your instance.
So, install docker in your instance using the below command:
sudo apt-get update && sudo apt-get install docker.io
Then add User to the group docker, by using the below command:
sudo usermod -aG docker $USER
To check User is added to the group or not, use the below command:
sudo cat /etc/group grep docker /etc/group Note: You can use one of the command to see if user has been added to the group or not.
Sometimes, User didn't get reflect to the group, then reboot your instance, using the below command:
sudo reboot
This #2 failed due to permission denied error to
/var/run/docker.sock
To solve this error, use the below commands:
sudo chown $USER /var/run/docker.sock sudo chmod 777 /var/run/docker.sock
So, finally we have green tick and the build is successful and in the final line of the Console Output, we can observe that Finished: Success is printed. That means our build is successful.
To verify that the application is accessible you can look for the container running and on browser you can access host_IP:8000. Remember to add port 8000 to security group inside
inbound rule
on your instance.
EXAMPLE - 2
We will create a Jenkins project to run the "docker-compose up -d" command to start the multiple containers defined in the compose file. Setting up with a cleanup step in the Jenkins project to run the "docker-compose down" command to stop and remove the containers defined in the compose file.
First install docker-compose on your instance using the below command:
sudo apt-get update && sudo apt-get install docker-compose
We are using the same above pipeline, just doing some changes, as seen below:
In the Add build steps of the configuration section, write in the following command and go ahead and build the project.
docker-compose down docker-compose up -d
Now, we will save the configuration and will run the build.
So, as you can see the build is successful.
To verify you can see container running on the instance.
So, these are the two examples on how you can use GitHub repo to fetch the code and deploy your application using docker commands, automating it through Jenkins.
Conclusion
In this blog, I have done a freestyle project on Jenkins to deploy a Django application on Docker. If you have any questions or would like to share your experiences, feel free to leave a comment below.
*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊
~Smriti Sharma✌