Automating Docker Tasks with Jenkins Freestyle Projects

Automating Docker Tasks with Jenkins Freestyle Projects

Day 23 & 24: Jenkins Freestyle Project for DevOps Engineers

ยท

3 min read

Introduction

Continuous Integration and Continuous Deployment (CI/CD) are integral practices in modern software development. In this comprehensive tutorial, we'll delve into the fundamentals of CI/CD, explore the concept of a build job, and master the creation of Jenkins Freestyle Projects to automate Docker tasks. Whether you're a novice or an experienced developer, this guide aims to demystify the process and empower you to streamline your development workflow effectively.

Understanding CI/CD ๐Ÿ”„

CI/CD involves automating the process of integrating code changes and deploying applications. By continuously integrating new code and deploying it seamlessly, CI/CD promotes efficiency, reliability, and faster delivery of software.

What is a Build Job? ๐Ÿ—๏ธ

Jenkins Freestyle Projects provide a user-friendly and flexible approach to configuring build jobs. These projects allow developers to set up automation tasks without the need for extensive scripting, making them an ideal starting point for CI/CD beginners.

Exploring Freestyle Projects ๐ŸŽจ

Jenkins Freestyle Projects are user-friendly and flexible, making them an excellent starting point for CI/CD beginners. These projects allow you to configure build jobs without needing extensive scripting.

For the tasks that follow, I have used the below projects:

  1. Containerizing a Django Todo Application

  2. Deploying a Two-Tier Flask App with MySQL Using Docker

Task-01: Building and Running Docker Containers ๐Ÿณ

Step 1: Create an Agent for Your App ๐Ÿ•ต๏ธโ€โ™‚๏ธ

  1. In Jenkins, go to "Manage Jenkins" > "Manage Nodes and Clouds."

  2. Click "New Node" to create a new agent. Give it a name, choose "Permanent Agent," and configure the necessary settings.

Step 2: Configure a Freestyle Project โš™๏ธ

Create a new Freestyle Project and specify the agent you created as the execution environment.

Step 3: Add Build Steps ๐Ÿ› ๏ธ

  1. In the project configuration, navigate to the "Build" section.

  2. Click on "Add build step" and choose "Execute shell" (for Unix-based systems) or "Execute Windows batch command" (for Windows).

  3. For the "Command," enter docker build -t your-app-image . to build your Docker image.

  4. Add a second build step by clicking "Add build step" again. Choose "Execute shell" or "Execute Windows batch command," and enter docker run -d -p 8001:8001 your-app-image to run your Docker container.

Step 4: Save and Build ๐Ÿ’พ

  1. Save your project configuration.

  2. Trigger a build by clicking "Build Now" on the project dashboard.

Jenkins will now execute the specified build steps, creating a Docker image and running a container for your app.

Step 5: Manual Browser Testing ๐ŸŒ

  1. Open your preferred web browser.

  2. Navigate to http://localhost:8080 to access your Todo App manually.

Task-02: Docker Compose Automation ๐Ÿค–

Step 1: Create a Jenkins Project ๐Ÿ—๏ธ

Create a new Freestyle Project following the steps above.

Step 2: Configure Build Steps โš™๏ธ

  1. Under the "Build" section, click "Add build step" and choose "Execute shell" or "Execute Windows batch command."

  2. Enter the command docker-compose -f path/to/your/docker-compose-file.yml up -d to start multiple containers defined in your Docker Compose file.

  3. Add a cleanup step by clicking "Add build step" again. Choose "Execute shell" or "Execute Windows batch command," and enter docker-compose -f path/to/your/docker-compose-file.yml down to stop and remove containers.

Step 3: Save and Run ๐Ÿ’พ

  1. Save your project configuration.

  2. Trigger a build to see Jenkins automating the deployment of your Docker Compose application.

Step 4: Manual Browser Testing for Two-Tier Flask App ๐ŸŒ

  1. Open your preferred web browser.

  2. Navigate to http://localhost:8080 to access your Two-Tier Flask App manually.

With these detailed steps, even beginners can successfully set up Jenkins Freestyle Projects for Docker automation. Remember, practice is key, so feel free to experiment with different configurations to enhance your CI/CD skills.

Happy Learning! ๐Ÿš€

ย