Run a Python App
The your python application up and running is as simple as packaging up the application into a Docker container. Once you have the Docker container uploaded to a container registry, you can easily deploy that container on 1-100s of fully customizable (# CPUs, # RAM, GPU) instances.
In this guide, we will create a simple Hello World
python application, but these steps can be used to deploy any new
or existing application on Salad.
Creating the Python Container
Requirements
You would normally have the package requirements for your application in some file.
It would depend mainly on the tool you use to install those requirements.
The most common way to do it is to have a file requirements.txt with the package names and their versions, one per line.
You would of course use the same ideas you read in About FastAPI versions to set the ranges of versions.
For example, your requirements.txt could look like:
And you would normally install those package dependencies with pip, for example:
Create the FastAPI Code
- Create a
main.py
file with:
Create the Dockerfile
Now in the same project directory create a file Dockerfile
with:
You should now have a directory structure like:
Build the Docker Image
Now that all the files are in place, let’s build the container image.
- Go to the project directory (in where your
Dockerfile
is, containing yourapp
directory). - Build your FastAPI image:
Test the Container Locally
Run the Docker Container
Run a container based on your image on your local machine:
Check the API
You should be able to check it in your Docker container’s URL, for example:
- http://localhost:80/
- http://localhost/items/2
You will see something like:
Push Container to a Registry
Now that you have a container locally, you will need to push that container image up to a container registry so it can be deployed on SaladCloud. You can push to any of the supported container registries, but for this guide we will use Docker Hub.
Tagging the Image
In order to push the container image to Docker Hub you will first need to tag your image with your Docker username and
an unique container name. The format of the image name is {docker-hub-username}/{image-name}:{version}
At SaladCloud we use the saladtechnologies
username, but you will need to use your own username.
Pushing the Image
Now that your container has the proper naming convention, you can push the container to Docker Hub by running the push
command.
Once the image is pushed to Docker Hub you can now deploy your container on Salad!
Deploy Container to Salad
Creating the Container Group
Now that you have a complete (yet basic) FastAPI application running you can deploy it to SaladCloud via the Portal or the Public API.
When you are asked to specify the “Image Source” you simply need to provide the image name you used to push the container up to the registry.
Since the FastAPI is listening to port 80, you will need to enable the Container Gateway and configure the port when creating a new container group.
Connecting to the Application
Once the container group is up and running, you will be given a static URL that load balances all http requests across
the instances of your applications. If you enabled authentication then you will need to include your API key in the
'Salad-Api-Key
header of your http requests.
Example Request
Congrats! You have successfully created, containerized, deployed and connected to your first application on Salad!
Was this page helpful?