Day 23 Task: Jenkins Freestyle Project for DevOps Engineers

Md AfrozMd Afroz
3 min read

Steps for Task 1: Automate Docker Build and Run in Jenkins

  1. Create a Freestyle Project in Jenkins:

    • Go to New Item in Jenkins, create a new Freestyle Project, and name it something like FlaskApp_Build_Run.
  2. Source Code Management:

    • In the Jenkins project configuration, if your project files (app.py, requirements.txt, Dockerfile) are in a Git repository, set up Source Code Management with the repository URL to pull the files directly.

    • If you’re uploading files manually, you can skip this part and ensure files are in the Jenkins workspace.

  3. Build Steps:

    • Step 1: Build the Docker Image:

      • Under Build > Add build step, select Execute Shell and enter the following command:

          bashCopy codedocker build -t flask_app_image .
        

        This command builds a Docker image with the name flask_app_image from your Dockerfile.

    • Step 2: Run the Docker Container:

      • Add another Execute Shell step with the following command:

          bashCopy codedocker run -d -p 8080:80 --name flask_app_container flask_app_image
        
        • This command runs a container from flask_app_image, maps port 80 in the container to 8080 on your local machine, and names it flask_app_container.
  4. Save and Build the Project:

    • Click Save and then Build Now to run the job.

    • After a successful build, you can access the application by going to http://<Jenkins_server_IP>:8080 (or localhost:8080 if Jenkins is on your local machine).


Steps for Task 2: Docker Compose Automation in Jenkins

  1. Create a Docker Compose File (docker-compose.yml):

    • In the same project directory, create a docker-compose.yml file if you haven’t done so already.

    • Here’s a sample docker-compose.yml to start both the Flask app and a PostgreSQL database:

        yamlCopy codeversion: '3'
        services:
          web:
            build: .
            ports:
              - "8080:80"
          db:
            image: postgres:13
            environment:
              POSTGRES_USER: user
              POSTGRES_PASSWORD: password
      
  2. Set Up Another Jenkins Freestyle Project:

    • Create a new Jenkins Freestyle Project and name it something like FlaskApp_DockerCompose.
  3. Source Code Management:

    • Configure Source Code Management as needed to ensure Jenkins has access to the docker-compose.yml file.
  4. Build Steps:

    • Step 1: Run docker-compose up:

      • Under Build > Add build step, select Execute Shell and add the following command:

          bashCopy codedocker-compose up -d
        
        • This command starts the containers defined in docker-compose.yml in detached mode.
    • Step 2: Cleanup with docker-compose down:

      • Add a Post-Build Action or additional Execute Shell step to stop and remove the containers after each build:

          bashCopy codedocker-compose down
        
        • This command stops and removes all the containers started by docker-compose up.
  5. Save and Build the Project:

    • Click Save and Build Now to start the job.

    • Check http://<Jenkins_server_IP>:8080 to ensure the application is accessible.


Testing and Verification

  • After running the first job (FlaskApp_Build_Run), visit http://localhost:8080 to verify that your Flask application is up and running.

  • After running the second job (FlaskApp_DockerCompose), the same URL should work, and you can also confirm that the database container is up by inspecting running containers with docker ps.

0
Subscribe to my newsletter

Read articles from Md Afroz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Afroz
Md Afroz