Deployment
Deploying a Starlette application is similar to deploying any other ASGI application.
Starlette provides an ASGI interface, but does not provide a production server.
To deploy your application, you will need an ASGI server such as Uvicorn, Hypercorn, or Daphne.
Deploying with Gunicorn
Gunicorn is a popular WSGI server that can be used with Uvicorn to manage worker processes.
To use Gunicorn with Uvicorn, you need to install both packages:
pip install gunicorn uvicorn
Then, you can run Gunicorn with the Uvicorn worker class:
gunicorn -k uvicorn.workers.UvicornWorker -w 4 -b 127.0.0.1:8000 main:app
This will start Gunicorn with 4 worker processes, each running a Uvicorn server.
- The
-k uvicorn.workers.UvicornWorker
parameter tells Gunicorn to use the Uvicorn worker class. - The
-w 4
parameter tells Gunicorn to start 4 worker processes. - The
-b 127.0.0.1:8000
parameter tells Gunicorn to bind to the specified address and port. - The
main:app
parameter tells Gunicorn to load theapp
application from themain
module.
Deploying with Uvicorn
Uvicorn is a fast ASGI server that can be used to run Starlette applications in production.
To use Uvicorn, you first need to install it:
pip install uvicorn
Then, you can run Uvicorn with your application:
uvicorn --host 0.0.0.0 --port 8000 main:app
This will start Uvicorn with a single worker process.
- The
--host 0.0.0.0
parameter tells Uvicorn to listen on all network interfaces. - The
--port 8000
parameter tells Uvicorn to listen on port 8000. - The
main:app
parameter tells Uvicorn to load theapp
application from themain
module.
Deploying with Nginx and Gunicorn
For production deployments, it is recommended to use a reverse proxy server like Nginx in front of Gunicorn.
Nginx can be used to handle HTTPS connections, serve static files, and load balance between multiple Gunicorn worker processes.
Here is an example Nginx configuration that proxies requests to Gunicorn:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
This configuration tells Nginx to listen on port 80 and forward all requests to Gunicorn, which is listening on port 8000.
Deploying with Docker
Deploying a Starlette application with Docker is a common way to ensure that the application runs consistently across different environments.
Creating a Docker image
To deploy a Starlette application with Docker, you need to create a Docker image that contains your application and its dependencies.
Here is an example Dockerfile
that can be used to create a Docker image for a Starlette application:
# Use the official Python image as a base image
FROM python:3.11
# Set the working directory in the container
WORKDIR /app
# Copy the application files into the container
COPY . /app
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose the port that the application will run on
EXPOSE 8000
# Run the application with Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
This Dockerfile
uses the official Python image as a base image, copies the application files into the image, installs the dependencies, and runs the application with Uvicorn.
Creating a Docker container
Once you have created a Docker image, you can use it to create a Docker container.
The following command will create a Docker container from the my-starlette-app
image and run it:
docker run -d -p 8000:8000 --name my-starlette-container my-starlette-app
This command will run a Docker container in the background, map port 8000 of the container to port 8000 of the host, and name the container my-starlette-container
.
Deploying on cloud services
You can also deploy your Starlette application to various cloud services such as Heroku, AWS, Google Cloud, etc.
The deployment process will vary depending on the cloud service you are using.
Generally, you will need to create a Procfile
or a similar configuration file to specify how to run your application.
For more information, please refer to the documentation of the cloud service you are using.