Skip to navigationSkip to main contentSkip to footerScaleway DocsSparklesIconAsk our AI
SparklesIconAsk our AI

Running a Python Flask Server on a Serverless Container

caasnodejsPythonFlask

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:

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

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
    CheckCircleOutlineIcon
    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
    CheckCircleOutlineIcon
    Tip

    The registry name starts with funcscw + the namespace name.

  3. Deploy the image using the Scaleway console.
Questions?

Visit our Help Center and find the answers to your most frequent questions.

Visit Help CenterArrowRightIcon
SearchIcon
No Results