run python flask server on serverless container

Jump toUpdate content

Run a Python Flask Server on a Serverless Container

Reviewed on 19 April 2022Published on 14 October 2021
  • serverless
  • containers
  • faas
  • functions
  • nodejs
  • express-server
  • Python
  • Flask

Flask is a Python framework that enables you to easily deploy simple web servers and APIs. In this tutorial, you will learn how to configure your Flask application to run on a Serverless Container.


Creating a Flask application

We first need to create a simple Web Server with Python and the Flask framework.

  1. Create a new folder for the project:

    mkdir my_python_project
  2. Use your favourite text editor to create a requirements.txt. In this tutorial, we use nano:

    nano requirements.txt
  3. Enter the following text in the requirements.txt file to define the library you want to import. Then save and quit the text editor:

  4. Create a server.py text file, and add the following code to it. This creates a simple web app using Flask:

    from flask import Flask
    import os
    import json

    DEFAULT_PORT = "8080"
    MESSAGE = "Hello, World from Scaleway Container !"

    app = Flask(__name__)

    def root():
    return json.dumps({
    "message": MESSAGE

    if __name__ == "__main__":
    # Scaleway's system will inject a PORT environment variable on which your application should start the server.
    port_env = os.getenv("PORT", DEFAULT_PORT)
    port = int(port_env)
    app.run(debug=True, host="", port=port)

Configuring your Docker image

In order to Dockerize our simple web app, we will use the official Python 3 Alpine image which is the smallest source image.

  1. Create a Dockerfile using the following command:

    touch Dockerfile

    A Dockerfile is a document that contains all the commands a user could call from the command line to create the image.

  2. In the Dockerfile, add the following;

    • Define the Python base image you want to use:
    FROM python:3-alpine
    • Create a directory to store your application code:
    # Create app directory
    WORKDIR /usr/src/app
    • Install the required python libraries (defined in the requirements.txt file):
    COPY requirements.txt .
    RUN pip install -qr requirements.txt
    • Copy your application server code inside the Docker image:
    COPY server.py .
    • Define the PORT environment variable and expose port 8080. This dictates the port the application will bind to:
    ENV PORT 8080
    EXPOSE 8080
    • Use CMD to run the python command that will start your application:
    CMD [ "python3", "./server.py" ]

    Your final Dockerfile should look like this:

    FROM python:3.9-alpine

    # Create app directory
    WORKDIR /usr/src/app

    # Expose Ports
    ENV PORT 8080
    EXPOSE 8080

    # Install libraries
    COPY requirements.txt .
    RUN pip install -qr requirements.txt
    COPY server.py .

    CMD ["python3", "./server.py"]

Deploying the application on Serverless Containers

  1. Build your image using the following command:

    docker build . -t <your application name>
  2. Push the created image into the container registry linked to your Containers namespace


    The registry name starts with funcscw + the namespace name.

  3. Deploy the image using the Scaleway console.