AI Deploy - Tutorial - Deploy a simple app with Flask

How to build and use a custom Docker image containing a Flask application

Last updated 31st January, 2023.

AI Deploy is in beta. During the beta-testing phase, the infrastructure’s availability and data longevity are not guaranteed. Please do not use this service for applications that are in production, as this phase is not complete.

AI Deploy is covered by OVHcloud Public Cloud Special Conditions.

Objective

Flask is an open-source micro framework for web development in Python.

The purpose of this tutorial is to show you how to build and use a custom Docker image for a Flask application.

Requirements

Instructions

Write a simple Flask application

Create a simple Python file with name app.py.

Inside that file, import your required modules:

from flask import Flask

Create Flask app:

app = Flask(__name__)

Define a simple function:

@app.route('/')
def index():
    return 'Web App with Python Flask using AI Deploy!'

Start your app:

if __name__ == '__main__':
    # starting app
    app.run(debug=True,host='0.0.0.0')
  • More information about Flask can be found here.
  • Direct link to the full python file can be found here here.

Write the requirements.txt for your applications

The requirements.txt file will allow us to write all the modules needed to make our application work. This file will be useful when writing the Dockerfile.

Flask==1.1.2

Write the Dockerfile for your application

Your Dockerfile should start with the the FROM instruction indicating the parent image to use. In our case we choose to start from a classic Python image.

FROM python

Install the requirements.txt file which contains your needed Python modules using a pip install ... command:

RUN pip install -r requirements.txt

Define your default launching command to start the application.

CMD [ "python" , "/workspace/app.py" ]

Give correct access rights to ovhcloud user (42420:42420):

RUN chown -R 42420:42420 /workspace
ENV HOME=/workspace
  • More information about Dockerfiles can be found here.
  • Direct link to the full Dockerfile can be found here here.

Build the Docker image from the Dockerfile

Launch the following command from the Dockerfile directory to build your application image:

docker build . -t flask-app:latest

The dot . argument indicates that your build context (place of the Dockerfile and other needed files) is the current directory.

The -t argument allows you to choose the identifier to give to your image. Usually image identifiers are composed of a name and a version tag <name>:<version>. For this example we chose flask-app:latest.

Please make sure that the docker image you will push in order to run containers using AI products respects the linux/AMD64 target architecture. You could, for instance, build your image using buildx as follows:

docker buildx build --platform linux/amd64 ...

Test it locally (optional)

Launch the following docker command to launch your application locally on your computer:

docker run --rm -it -p 5000:5000 --user=42420:42420 flask-app:latest

The -p 5000:5000 argument indicates that you want to execute a port rediction from the port 5000 of your local machine into the port 5000 of the docker container. The port 5000 is the default port used by Flask applications.

Don't forget the --user=42420:42420 argument if you want to simulate the exact same behavior that will occur on AI Deploy apps. It executes the docker container as the specific OVHcloud user (user 42420:42420).

Once started, your application should be available on http://localhost:5000.

Push the image into the shared registry

The shared registry of AI Deploy should only be used for testing purposes. Please consider attaching your own docker registry. More information about this can be found here.

Find the address of your shared registry by launching this command:

ovhai registry list

Log in on the shared registry with your usual OpenStack credentials:

docker login -u <user> -p <password> <shared-registry-address>

Push the compiled image into the shared registry:

docker tag flask-app:latest <shared-registry-address>/flask-app:latest
docker push <shared-registry-address>/flask-app:latest

Launch the AI Deploy app

The following command starts a new app running your Flask application:

ovhai app run --default-http-port 5000 --cpu 1 <shared-registry-address>/flask-app:latest

--default-http-port 5000 indicates that the port to reach on the app url is the 5000.

--cpu 1 indicates that we request 1 cpu for that app.

Consider adding the --unsecure-http attribute if you want your application to be reachable without any authentication.

Once the app is running you can access your Flask application directly from the app's URL.

image

Go further

  • To go further with Flask, imagine creating an app to deploy an Object Detection model. Refer to this tutorial.
  • Flask allows you to do sentiment classification on texts using Hugging Face models. Here it is.

Feedback

Please send us your questions, feedback and suggestions to improve the service:


Questa documentazione ti è stata utile?

Prima di inviare la valutazione, proponici dei suggerimenti per migliorare la documentazione.

Immagini, contenuti, struttura... Spiegaci perché, così possiamo migliorarla insieme!

Le richieste di assistenza non sono gestite con questo form. Se ti serve supporto, utilizza il form "Crea un ticket" .

Grazie per averci inviato il tuo feedback.


Potrebbero interessarti anche...

OVHcloud Community

Accedi al tuo spazio nella Community Fai domande, cerca informazioni, pubblica contenuti e interagisci con gli altri membri della Community OVHcloud

Discuss with the OVHcloud community

Conformemente alla Direttiva 2006/112/CE e successive modifiche, a partire dal 01/01/2015 i prezzi IVA inclusa possono variare in base al Paese di residenza del cliente
(i prezzi IVA inclusa pubblicati includono di default l'aliquota IVA attualmente in vigore in Italia).