The aim of this guide is to walk you through containerising a generic Python application and getting it built and deployed on Northflank. You can replace FastAPI with OpenAI or whatever else you prefer!
You’ll need to :northflank: link your Git account to your team first, or just fork our example repo.
The first step to containerising your code is writing a Dockerfile. This should live in your Git repository alongside the rest of your app.
A Dockerfile is a list of instructions Northflank will use to build your code into a reusable image that can be deployed anywhere.
This example can be found on GitHub at https://github.com/northflank-examples/fastapi-docker
For this very basic example, assume we have a directory structure such as:
my-fastapi-app/
|- app/
|- main.py
|- Dockerfile
|- requirements.txt
Where our main.py is a super simple FastAPI app:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return "Hello World"
To write a Dockerfile for this app, we need to tell Docker what the container should be based on, what files to copy into the container, any installation/build steps to be run, and finally the command to run when the container starts up.
For the code above, our Dockerfile could look like:
# Start from the offical Python 3.9 base image.
# This will include all the basic components we need.
FROM python:3.9
# Within the container, we'll work in this directory.
WORKDIR /code
# Copy our requirements.txt file into the container.
COPY ./requirements.txt requirements.txt
# Install our requirements within the container.
RUN pip install --no-cache-dir --upgrade -r requirements.txt
# Copy our source code directory into the container.
COPY ./app app
# Expose port 80 within our container to the outside world.
EXPOSE 80
# Specify the command to run when the container is started.
# In this case, start our FastAPI server on port 80.
CMD ["fastapi", "run", "app/main.py", "--port", "80"]
With a Dockerfile committed to our repository, we are ready to deploy our app on Northflank.
create new → service → combined service. A combined service both builds and deploys your code, giving you full CI/CD in one.