Building a Simple Flask App with Docker and GitHub Actions: A Complete Guide

In the world of modern software development, automation and containerization have become essential tools for building, testing, and deploying applications efficiently. In this article, I’ll walk you through how I built a simple Flask web application, containerized it using Docker, and set up a CI/CD pipeline using GitHub Actions.
Whether you're new to DevOps or just looking to refresh your skills, this guide will help you understand the basics of integrating these technologies into your workflow.
The Project
This project includes the following key components:
A simple Flask Web Application that returns a “Hello there, DevOps!!!” message
A Docker container that packages and runs the app
Version Control with Docker
A CI/CD pipeline using GitHub Actions, which automatically builds and pushes the Docker image to Docker Hub
Create a Simple Flask Application
I started by creating a basic Flask app in Python. I needed to set up a single route that returns a simple message: Hello there, DevOps!!!
webapp.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello there, DevOps!!!\n"
if __name__ in "__main__":
app.run(host='0.0.0.0', port=5000)
To run this locally, I used the below on the terminal:
pip install flask
python webapp.py
I went to a new tab on my browser and put into the URL: http://localhost:5000
. The message “Hello there, DevOps!!!” was displayed. This means the app ran without errors.
Containerize the Application
Next, I need to make the app portable and easy to deploy. For this, I used Docker.
Dockerfile
FROM python:3.11-alpine
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
WORKDIR /app
RUN pip install --upgrade pip
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/
EXPOSE 5000
CMD ["python", "webapp.py"]
I also created a .dockerignore
file to prevent unnecessary files from being included in the image:
*.pyc
__pycache__
env/
.git
README.md
After building the Docker image:
docker build -t webapp:1.0 .
And running it:
docker run -d -p 5000:5000 webapp:1.0
Now the app was running inside a container, and I could access it at http://localhost:5000
.
Set Up Version Control
I initialized a Git repository in my local IDE by using the command git init
, and then I pushed the code to a Github repository, making it easier to collaborate and track changes over time.
Implement CI/CD with GitHub Actions
The final step was to automate the process using GitHub Actions. I created a workflow that:
Builds the Docker image
Pushes it to Docker Hub
To do this, I had to open an account with Docker Hub and generate docker tokens to be used in the workflow as GitHub Secrets.
Lessons Learned
This project gave me hands-on experience with several important DevOps tools:
Flask: For building a lightweight web application.
Docker: For packaging and running the app in an isolated environment.
GitHub: For version control and collaboration.
GitHub Actions: For automating the build and deployment process.
By combining these tools, I was able to create a fully automated pipeline that makes it easy to test, build and deploy the app whenever changes are made.
Conclusion
Building a simple Flask app might seem basic, but when combined with Docker and GitHub Actions, it becomes a powerful foundation for more complex projects. This project has given me the skills needed to streamline and automate my workflow which is crucial in modern software development.
You can find the full source code on GitHub. All you need to do is clone it and follow the instructions in the Readme.md file.
Subscribe to my newsletter
Read articles from Omiete directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by