Flask Docker Github Actions

Automating Flask Deployment Using Docker and Github Actions

I’ve had enough of tedious deployments, so I decided to become proficient in automating the process. While I’ve previously used GitHub Actions to streamline deployments, I had yet to do so in a Flask environment. In this article, I’ll outline the steps I followed to deploy my Flask backend using Docker and GitHub Actions, specifically focusing on Flask Docker GitHub Actions.

1. Creating a Flask Application for Docker Deployment

The first step is to create a simple Flask application. The default Flask homepage example works perfectly, but I modified it to use a different port to avoid conflicts.

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

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

2. Setting Up the Dockerfile for Your Flask Docker App

Using Docker is one of the simplest ways to deploy our application. It not only eases the deployment process but also allows us to automate it effectively. Flask requires a WSGI server for production deployment. In this article, I’ll be using Gunicorn as the WSGI server.

Now, let’s talk about Flask production deployment before we get into Docker. Similar to Django, Flask requires a WSGI server in order to launch the program. The two most popular WSGI servers are Gunicorn and UWSGI. I won’t delve into specifics about how they differ from one another. Gunicorn will be used as the WSGI server in this article.

Here is the Dockerfile.

Dockerfile

FROM python:3.12-slim

# Then, we need to copy requirements.txt
COPY requirements.txt /tmp/requirements.txt

# Last, we install the dependency and then we can start the Gunicorn.
RUN pip install -r /tmp/requirements.txt

COPY . /tmp/app

WORKDIR /tmp/app

CMD ["gunicorn", "--bind", "0.0.0.0:5001", "app:app"]

Remember to include flask and gunicorn in requirements.txt.

You can build and run the Dockerfile using the following commands

docker build -t flask-deployment .
docker run -dp 5001:5001 flask-deployment

3. Automating Flask Deployment with GitHub Actions for Docker

Now that we have our Dockerfile ready, it’s time to automate the deployment with GitHub Actions. Here’s a simple workflow to achieve this

name: deploy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: self-hosted
    steps:
    - uses: actions/checkout@v4
    - name: Build and Run Docker
      run: docker build -t tutorial/flask-deployment . && docker run -dp 5001:5001 tutorial/flask-deployment

This setup triggers the workflow only when changes are pushed to the master branch and runs on my personal virtual machine.

After committing the deploy.yml file, use curl localhost:5001 from your server. You should see “Hello, World!”—an indication that your Flask app is up and running, and the automation is working!

Troubleshooting Flask Docker Github Actions

If you encounter any issues during deployment, double-check your configurations and logs to identify the root cause.

Conclusion

Automating Flask deployment using Docker and GitHub Actions not only saves time but also reduces errors in the deployment process. By following these steps, you can ensure a smoother workflow for your projects.

Comments

Leave a Reply