Skip to content

Hosting a Flask Web App on TrueNAS SCALE 24.10 Using Docker

Overview

This guide will walk you through hosting a Flask web application on TrueNAS SCALE 24.10, which supports Docker. We will create a sample Flask application, build a Docker image, push it to Docker Hub, and then deploy it using the TrueNAS SCALE UI.

Step 1: Create a Sample Flask Application

On your local machine, create a directory for your project:

mkdir flask-app && cd flask-app

Create a Python file named app.py inside this directory:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, TrueNAS!"

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

Step 2: Create a Dockerfile

In the same directory as app.py, create a Dockerfile:

# Use an official Python runtime as a base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the application files
COPY app.py /app/

# Install Flask
RUN pip install flask

# Expose port 5000 for Flask
EXPOSE 5000

# Define the command to run the app
CMD ["python", "app.py"]

Step 3: Build and Push the Docker Image

First, log in to Docker Hub (assuming you have an account):

docker login

Then, build the Docker image:

docker build -t <your-dockerhub-username>/flask-app:latest .

Once the image is built, push it to Docker Hub:

docker push <your-dockerhub-username>/flask-app:latest

Alternative: Hosting Locally on TrueNAS

If you prefer to host the image locally instead of using Docker Hub, you need to ensure TrueNAS SCALE can accept insecure Docker registries:

  1. Enable Insecure Registries in TrueNAS:
  2. Go to System Settings > Advanced
  3. Scroll to Docker Configuration and add your local registry
  4. Restart the Docker service

  5. Tag and Push to Local Registry:

    docker tag flask-app localhost:5000/flask-app:latest
    docker push localhost:5000/flask-app:latest
    


Step 4: Deploy the Flask App on TrueNAS SCALE

4.1 Open TrueNAS SCALE Apps

  1. Navigate to Apps > Custom Apps.
  2. Click Add to create a new container app.

4.2 Configure Container Settings

  • Application Name: flask-app
  • Container Image: <your-dockerhub-username>/flask-app:latest
  • Container Port: 5000
  • Host Port: 15000
  • Enable Networking: Ensure the app is accessible within the LAN

4.3 Deploy and Access the App

  1. Click Save & Deploy.
  2. Once running, access the Flask app at:
    http://<truenas-ip>:15000/
    

Conclusion

You have successfully deployed a Flask web app on TrueNAS SCALE 24.10 using Docker. You can now develop, build, and update the application by pushing new versions to Docker Hub or hosting it locally with an insecure registry setup.