πŸš€ Dockerized Flask App Deployment via Jenkins CI/CD on Localhost

Harshitha G MHarshitha G M
2 min read

Automate Docker image creation and deployment for a Python Flask app using Jenkins pipelines, built from scratch on WSL2.

🧠 Introduction

As part of my DevOps learning journey, I decided to take on a real-world automation challenge:

πŸ“Œ Build a Jenkins pipeline from scratch to deploy a Dockerized Flask app on localhost!

β€œAutomate Docker Image Creation and Deployment using Jenkins Pipeline β€” no pre-built images allowed.”

πŸ”§ What I Built

βœ… Project Overview

  • Build a Flask app Docker image

  • Automate the entire process using Jenkins pipeline

  • Expose the container at localhost:5000

  • Push image to self-hosted registry (optional)

🧰 Tech Stack

ToolPurpose
🐍 Python/FlaskLightweight web application
🐳 DockerContainerization
🧱 JenkinsCI/CD pipeline automation
🐧 Ubuntu WSL2Local DevOps lab setup

πŸ“ File Structure

πŸ“¦ flask-docker-jenkins/
 ┣ πŸ“‚ app/
 ┃ β”— πŸ“„ app.py
 ┣ πŸ“„ Dockerfile
 ┣ πŸ“„ Jenkinsfile

πŸ’‘ The Flask App (app.py)

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "βœ… Hello from Flask App via Docker + Jenkins!"

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

🐳 Dockerfile

FROM python:3.9
WORKDIR /app
COPY ./app /app
RUN pip install -r requirements.txt
CMD ["python3", "app.py"]

βš™οΈ Jenkinsfile (CI/CD Pipeline) - get it from github -https://github.com/HARSHITHA-G-M/Automate-Docker-Image-Creation-and-Deployment-using-Jenkins-Pipeline.git

πŸ˜΅β€πŸ’« Errors I Faced (and Fixed)

❌ Jenkins service failed to start

βœ… Fixed by installing correct Java version:

sudo apt install openjdk-17-jdk -y

❌ App not showing at localhost:5000

βœ… Realized Jenkins was only building the image, not running it. Added docker run in pipeline.

❌ Port conflict:

Bind for 0.0.0.0:5000 failed: port is already allocated

βœ… Found out registry container was using it. Stopped it:

docker stop registry && docker rm registry

βœ… Final Output

Once fixed, the Jenkins pipeline:

  • Builds the Docker image

  • Cleans up old containers

  • Runs the Flask app

  • App accessible at:
    πŸ”— http://localhost:5000

πŸ“Œ Key Takeaways

βœ… Docker alone isn't enough β€” CI/CD automation is where DevOps shines
βœ… Always check running containers with docker ps
βœ… Port conflicts are real and sneaky β€” clean up before re-running
βœ… Jenkins won't run your app unless you explicitly add that step!
βœ… Working from scratch gives you production-like debugging experience

🧠 Lesson You Learned (and Should Highlight in Blog/LinkedIn)

β€œEven if CI/CD automation works at script level, we must validate app health post-deployment β€” especially in local setups where ports and services can clash.”

0
Subscribe to my newsletter

Read articles from Harshitha G M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshitha G M
Harshitha G M

I am a DevOps enthusiast with hands-on experience in CI/CD pipelines, containerization, and automation using tools like Jenkins, Docker, Kubernetes, and Ansible. I work on building and deploying Flask-based applications and configuring end-to-end DevOps workflows from scratch. Passionate about cloud-native technologies and automation, I focus on optimizing deployment processes and ensuring smooth software delivery.