DEV Community

Cover image for Ecommerce App Deployment with Flask (Python), GitHub, Jenkins, Docker, and EC2. ๐Ÿš€
Yusra Liaqat
Yusra Liaqat

Posted on

Ecommerce App Deployment with Flask (Python), GitHub, Jenkins, Docker, and EC2. ๐Ÿš€

Hey folks! ๐Ÿ‘‹ Iโ€™m super excited to share an end-to-end DevOps project I recently completed. This project ties together AWS EC2, Docker, GitHub, and Jenkins into a seamless CI/CD workflow. If you're looking for a hands-on guide with a real-world use case, buckle up. I'll walk you through every step I took, from setting up infrastructure to deploying a containerized app with Jenkins. Letโ€™s dive in! ๐Ÿคฟ

Table of Contents

  1. The Idea
  2. Setting Up AWS EC2 Instances
  3. Installing Docker on the Ubuntu EC2 Instance
  4. Building the Ecommerce App
  5. Creating Repo locally and initializing on GitHub
  6. Pulling Jenkins and Installing Plugins
  7. Setting Up Jenkins with GitHub
  8. Creating a Jenkins Freestyle Pipeline
  9. Resolving Docker Issues
  10. Application Deployment
  11. Visualizing the Workflow
  12. Code and Resources
  13. Closing Thoughts

The Idea:

The goal was to build and deploy an ecommerce application using modern DevOps tools and practices. The process included application development, containerization, and setting up a pipeline for automated deployment.

Setting Up AWS EC2 Instances

I created two EC2 instances on AWS:

Ubuntu Instance: For running Docker and hosting the application.
Windows Instance: For developing the ecommerce app.
Both instances were provisioned with security groups to allow SSH/RDP and HTTP access.

Installing Docker on the Ubuntu EC2 Instance

To containerize and run our app, I installed Docker on the Ubuntu EC2 server. Here's the magic command I used:

sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Building the Ecommerce App

The Windows EC2 instance served as my dev environment. I developed a simple ecommerce app using Flask python. Once I was happy with the app, I pushed the code to GitHub.

Creating Repo locally and initializing on GitHub

Essentially, it is just as if you were creating your development project locally and then uploading it to Git

  • Create any Repo/Project locally in VS code
  • It wonโ€™t be a part of GitHub Repo as for now
  • After adding files your project and updating them we need to initialize our Repo so Git can recognize it
echo "# terraform01" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Yusra310/terraform01.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

This made my workflow a little cleaner and portable.

Pulling Jenkins and Installing Plugins

To automate the build and deployment process, I pulled the official Jenkins Docker image on the Ubuntu instance:

docker run -d --name jenkins \
  -p 8080:8080 -p 50000:50000 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

Enter fullscreen mode Exit fullscreen mode

I accessed Jenkins through the browser

http://54.159.138.147:8080
Enter fullscreen mode Exit fullscreen mode

unlocked it using the initial password, and installed the necessary plugins:

GitHub Plugin for GitHub integration.
Docker Plugin to enable Docker commands within Jenkins.

Setting Up Jenkins with GitHub

To securely integrate GitHub with Jenkins, I created a GitHub Personal Access Token. This token was added as a credential in Jenkins, allowing the CI/CD pipeline to pull code from the GitHub repository.

Creating a Jenkins Freestyle Pipeline

Hereโ€™s the exciting part! ๐ŸŽ‰ I set up a Freestyle Jenkins pipeline for the CI/CD workflow. The pipeline:

Jenkins Shell Script:

Docker build -t ecommerce-app
Docker run -p 5000:5000 -d ecommerce-app
Enter fullscreen mode Exit fullscreen mode

Resolving Docker Issues

The first pipeline run failed with an error: docker: command not found.
The issue? Docker wasnโ€™t installed in the Jenkins container! ๐Ÿคฆโ€โ™€๏ธ
Solution: I installed Docker inside the Jenkins container and gave it access to the hostโ€™s Docker socket. This allowed Jenkins to execute Docker commands seamlessly.

Application Deployment

With everything in place, I ran the Jenkins pipeline again and The ecommerce app was deployed and accessible in the browser at

http://54.159.138.147:5000
Enter fullscreen mode Exit fullscreen mode

Visualizing the Workflow

To make things more digestible, I created a draw.io diagram that outlines the entire workflow, from code push to deployment. Here's a summary of the steps:

  1. Develop the app on the Windows instance.
  2. Push the code to GitHub.
  3. Jenkins pulls the code, builds a Docker image, and runs the container.
  4. The app is hosted on the Ubuntu EC2 instance.

Code and Resources

Iโ€™ve shared the source code, Jenkins configuration, and the draw.io diagram here on GitHub [https://github.com/Yusra310/ecommerce-app]. Feel free to check it out and try this workflow yourself.

Closing Thoughts

This project was a blast to work on! It taught me the power of automation and containerization in modern application development. If youโ€™re just starting with DevOps, projects like this are a fantastic way to get hands-on experience.
Got questions or suggestions? Drop them in the comments below! Letโ€™s keep building. ๐Ÿ’ช
Until next time, happy coding and deploying! ๐Ÿ–ค

Top comments (0)