---
title: "AI Deploy - Tutorial - Deploy a simple app with Flask"
description: "Learn how to build and use a custom Docker image for a Flask application. Flask is an open-source micro framework for web development in Python."
url: https://docs.us.ovhcloud.com/en/public-cloud/ai-machine-learning/ai-deploy-build-use-flask-image
lang: en
---
> For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt.

# AI Deploy - Tutorial - Deploy a simple app with Flask

**Learn how to build and use a custom Docker image for a Flask application. [Flask](https://flask.palletsprojects.com/en/2.0.x/) is an open-source micro framework for web development in Python.**

**Note:**

AI Deploy is covered by **[OVHcloud Public Cloud Special Conditions](https://us.ovhcloud.com/legal/service-specific-terms/)**.

 

## Requirements

- Access to the [OVHcloud Control Panel](https://manager.us.ovhcloud.com/)
- An **AI Deploy project** created inside a [Public Cloud project](https://us.ovhcloud.com/public-cloud/) in your OVHcloud account
- A [user for AI Deploy](https://docs.us.ovhcloud.com/en/public-cloud/ai-machine-learning/manage-users-roles.md)
- [Docker](https://www.docker.com/get-started) installed on your local computer
- Some knowledge about building image and [Dockerfile](https://docs.docker.com/engine/reference/builder/)

 

## Instructions

### Write a simple Flask application

Create a simple Python file with the name `app.py`.

Inside that file, import your required modules:

```python
from flask import Flask
```

Create Flask app:

```python
app = Flask(__name__)
```

Define a simple function:

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

Start your app:

```python
if __name__ == '__main__':
    # starting app
    app.run(debug=True,host='0.0.0.0')
```

- More information about Flask can be found [here](https://flask.palletsprojects.com/en/2.0.x/).
- A direct link to the full Python file can be found [here](https://github.com/ovh/ai-training-examples/tree/main/apps/flask/hello-world).

 

### 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`.

```console
Flask==1.1.2
```

 

### Write the Dockerfile for your application

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

```console
FROM python
```

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

```console
RUN pip install -r requirements.txt
```

Define your default launching command to start the application.

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

Give correct access rights to OVHcloud user `(42420:42420)`:

```console
RUN chown -R 42420:42420 /workspace
ENV HOME=/workspace
```

- More information about Dockerfiles can be found [here](https://docs.docker.com/engine/reference/builder/).
- A direct link to the full Dockerfile can be found [here](https://github.com/ovh/ai-training-examples/blob/main/apps/flask/hello-world/Dockerfile).

 

### Build the Docker image from the Dockerfile

From the directory containing your **Dockerfile**, run one of the following commands to build your application image:

```console

# Build the image using your machine's default architecture

docker build . -t flask-app:latest

# Build image targeting the linux/amd64 architecture

docker buildx build --platform linux/amd64 -t flask-app:latest .
```

- The **first command** builds the image using your system’s default architecture. This may work if your machine already uses the `linux/amd64` architecture, which is required to run containers with our AI products. However, on systems with a different architecture (e.g. `ARM64` on `Apple Silicon`), the resulting image will not be compatible and cannot be deployed.

- The **second command** explicitly targets the `linux/AMD64` architecture to ensure compatibility with our AI services. This requires `buildx`, which is not installed by default. If you haven’t used `buildx` before, you can install it by running: `docker buildx install`

**Note:**

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

**Note:**

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**.

 

Test it locally (optional)

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

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

**Note:**

The `-p 5000:5000` argument indicates that you want to execute a port redirection 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.

**Warning:**

**NOTE:** 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

**Warning:**

**NOTE:** The shared registry should only be used for testing purposes. Please consider creating and attaching your own registry. More information about this can be found [here](https://docs.us.ovhcloud.com/en/public-cloud/ai-machine-learning/manage-registries.md). The images pushed to this registry are for AI Tools workloads only, and will not be accessible for external uses.

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

```console
ovhai registry list
```

Log in on the shared registry with your usual AI Platform user credentials:

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

Push the compiled image into the shared registry:

```console
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:

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

**Note:**

`--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.

 

## Go further

- **Flask** allows you to do sentiment classification on texts using Hugging Face models. [Here it is](https://docs.us.ovhcloud.com/en/public-cloud/ai-machine-learning/ai-deploy-flask-hugging-face-sentiment-analysis.md).

Browse all the [AI & Machine Learning guides](https://docs.us.ovhcloud.com/en/public-cloud/ai-machine-learning/landing-page-ai-machine-learning.md).

Browse the rest of the [OVHcloud US documentation](https://docs.us.ovhcloud.com/en/index.md) for guides on our other products and services.

Learn more about [AI & Machine Learning](https://us.ovhcloud.com/public-cloud/ai-machine-learning/) on the OVHcloud US website.

If you need training or technical assistance to implement our solutions, contact your sales representative or click on [this link](https://us.ovhcloud.com/professional-services/) to get a quote and ask our Professional Services experts for a custom analysis of your project.
