A Dockerfile is a document that contains all the commands a user could call from the command line to create the image.
Jump toSuggest an edit
Running a Python Flask Server on a Serverless Container
Reviewed on 25 June 2024 • Published on 14 October 2021
- 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.
- Create a new folder for the project:
mkdir my_python_project
- Use your favorite text editor to create a
requirements.txt
. In this tutorial, we use nano:nano requirements.txt - 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 - Create a
server.py
text file, and add the following code to it. This creates a simple web app using Flask:from flask import Flaskimport osimport jsonDEFAULT_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.
-
Create a Dockerfile using the following command:
touch DockerfileTip -
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 directoryWORKDIR /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 8080EXPOSE 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 directoryWORKDIR /usr/src/app# Expose PortsENV PORT 8080EXPOSE 8080# Install librariesCOPY requirements.txt .RUN pip install -qr requirements.txtCOPY server.py .CMD ["python3", "./server.py"]
Deploying the application on Serverless Containers
- Build your image using the following command:
docker build . -t <your application name>
- Push the created image into the container registry linked to your Containers namespace
Tip
The registry name starts with
funcscw
+ the namespace name. - Deploy the image using the Scaleway console.
Was this page helpful?