Jenkins Freestyle Project for DevOps Engineers

Jenkins Freestyle Project for DevOps Engineers

·

2 min read

In today's blog we will be using jenkins to build containers using:

  1. Docker file

  2. 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

  1. Make sure that docker is installed along with Jenkins

  2. Create a new job, let us name this job react_django_todo_app and select Freestyle project, then click "OK"

  3. In the General section, add a description to the project and provide the GitHub URL

  4. In the Source Code Management section, add the GitHub repository URL and the branch name

  5. In the build steps, select "add built steps" and write the shell script to automate the process of building the docker container.

    1. First, the code is cloned from GitHub since we have added the GitHub URL in the previous steps.

    2. 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
      
    3. Next, click "Save" and build the job

    4. 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

  1. Start with creating a new job, let us name this django-todo-cicd and use a freestyle project

  2. Provide the project description and github link of project

  3. Add the github repository url in Source Code Management section and specify the branch name

  4. 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 file

    Sometimes, 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 dependencies

  5. Now, 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