Title: Automating Flask Deployment Using Docker and Github Actions
Author: Immanuel Raj
Published: September 23, 2024
Last modified: February 15, 2025

---

![Flask Docker Github Actions](https://immanuelraj.dev/wp-content/uploads/2024/09/
Automating-Flask-Deployment-Using-Docker-and-Github-Actions.jpeg)

# Automating Flask Deployment Using Docker and Github Actions

[September 23, 2024](https://immanuelraj.dev/automating-flask-deployment-using-docker-and-github-actions/)

—

by

[Immanuel Raj](https://immanuelraj.dev/author/iamimmanuelraj/)

in [Docker](https://immanuelraj.dev/category/docker/), [Github Actins](https://immanuelraj.dev/category/github-actins/),
[Self Hosting](https://immanuelraj.dev/category/self-hosting/)

Read Time

2–3 minutes

[I’ve ](https://immanuelraj.dev)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**

    ```wp-block-code
    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](https://www.docker.com/) 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**

    ```wp-block-code
    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

    ```wp-block-code
    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

    ```wp-block-code
    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.

[automation](https://immanuelraj.dev/tags/automation/) [cicd](https://immanuelraj.dev/tags/cicd/)
[docker](https://immanuelraj.dev/tags/docker/) [flask](https://immanuelraj.dev/tags/flask/)
[github](https://immanuelraj.dev/tags/github/) [github actions](https://immanuelraj.dev/tags/github-actions/)
[python](https://immanuelraj.dev/tags/python/)

[Previous:  Retrofitting Node 20 in Ubuntu 18 LTS](https://immanuelraj.dev/retrofitting-node-v20-12-2-and-npm-v10-5-0-in-ubuntu-18-04-3-lts/)

[Next:  Why Do We Need God? Understanding Christian Faith and Purpose](https://immanuelraj.dev/why-we-need-god-and-finding-purpose-in-faith/)

![Immanuel Raj Avatar](https://secure.gravatar.com/avatar/88db6e1fa27cf854075acbaa156189ace30cf3701b2d8640cd774280ead1d4d3?
s=80&d=mm&r=g)

## About the author

Software Developer & Technology Consultant

---

## Popular Categories

 * [Bible](https://immanuelraj.dev/category/bible/) (1)
 * [Cloudflare](https://immanuelraj.dev/category/cloudflare/) (1)
 * [Databases](https://immanuelraj.dev/category/databases/) (1)
 * [Docker](https://immanuelraj.dev/category/docker/) (1)
 * [Email](https://immanuelraj.dev/category/email/) (1)
 * [ERPNext](https://immanuelraj.dev/category/erpnext/) (3)
 * [Frappe](https://immanuelraj.dev/category/frappe/) (2)
 * [Github Actins](https://immanuelraj.dev/category/github-actins/) (1)
 * [God](https://immanuelraj.dev/category/god/) (1)
 * [Google Cloud](https://immanuelraj.dev/category/gcp/) (1)
 * [Hosting](https://immanuelraj.dev/category/hosting/) (2)
 * [Life](https://immanuelraj.dev/category/life/) (1)
 * [Linux](https://immanuelraj.dev/category/linux/) (13)
 * [ML](https://immanuelraj.dev/category/ml/) (1)
 * [Networking](https://immanuelraj.dev/category/networking/) (2)
 * [Security](https://immanuelraj.dev/category/security/) (2)
 * [Self Hosting](https://immanuelraj.dev/category/self-hosting/) (7)
 * [SSL](https://immanuelraj.dev/category/ssl/) (3)
 * [Terminal](https://immanuelraj.dev/category/terminal/) (1)
 * [Tools](https://immanuelraj.dev/category/tools/) (2)
 * [Uncategorized](https://immanuelraj.dev/category/uncategorized/) (8)
 * [Web](https://immanuelraj.dev/category/web/) (2)
 * [WordPress](https://immanuelraj.dev/category/wordpress/) (1)

---

## Useful Links

Links I found useful and wanted to share.

 * [Sponsor Me](https://github.com/sponsors/iamimmanuelraj)

---

## Search the website

Search