In today's blog we will be using jenkins to build containers using:
Docker file
Docker-compose
Creating a container using docker-file
Let us create a freestyle project for a react-django-todo-app. The github link for this project is https://github.com/sowmya-bm/django-todo-cicd
Make sure that docker is installed along with Jenkins
Create a new job, let us name this job react_django_todo_app and select Freestyle project, then click "OK"
In the General section, add a description to the project and provide the GitHub URL
In the Source Code Management section, add the GitHub repository URL and the branch name
In the build steps, select "add built steps" and write the shell script to automate the process of building the docker container.
First, the code is cloned from GitHub since we have added the GitHub URL in the previous steps.
Next, we add the commands to create a docker image and container
echo "code cloned" docker build . -t django-todo-app-delivery # command to create docker image echo "code built" docker run -d -p 8001:8001 django-todo-app-delivery:latest # command to create docker container
Next, click "Save" and build the job
We can see that the job is successfully built and we can access the application on the port 8001
Creating a container using docker-compose
Now let us use Jenkins to build a docker container using docker-compose for a django-todo-cicd app. This application has a MySQL database and a volume attached to the container, hence docker-compose can be used. The link to thi sproject is https://github.com/sowmya-bm/django-todo-cicd
Start with creating a new job, let us name this django-todo-cicd and use a freestyle project
Provide the project description and github link of project
Add the github repository url in Source Code Management section and specify the branch name
Next, in the build steps, select "Execute shell" and add the shell script needed to create a docker container using docker-compose and click "Save"
Here
docker-compose down
is used as a clean up step to remove or kill the containers that are present due to docker fileSometimes, Jenkins fails to refresh containers during every build, so we specify
docker-compose up -d --no-deps --build django-todo-app my_sql_db
which makes sure that container is built without any dependenciesNow, when we click on "Build now", we can see that job is successfully built and we will be able to access the todo application on port no 8000