NavigationContentFooter
Jump toSuggest an edit

Running a Python Flask Server on a Serverless Container

Reviewed on 18 December 2023Published on 14 October 2021
  • serverless
  • containers
  • caas
  • 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.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • A Serverless Containers namespace
  • Installed Python 3, pip, and Docker on your computer

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 favorite 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:
    Flask==2.2.1
  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__)
    @app.route("/")
    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="0.0.0.0", 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
    Tip

    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
    Tip

    The registry name starts with funcscw + the namespace name.

  3. Deploy the image using the Scaleway console.
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway