/FastAPIDocker

A project to test FastAPI and Docker

Primary LanguagePython

FastAPIDocker

A project to test FastAPI and Docker.

Language: Python

Start: 2023

Why

I wanted to test FastAPI to create a simple API server. I took this opportunity also to try Docker. Therefore, I packaged the Python project in a Docker container and moved it to a Google Cloud machine.

Example of output of one of the get command:

output

Steps

Create the environment

  • python3 -m venv venv
  • . venv/bin/activate
  • pip install (all necessary packages including -of course- FastAPI)
  • pip freeze > requirements.txt

Build the container image

  • docker build -t python-fastapi .

Start the container

  • docker run -p 8000:8000 python-fastapi
  • docker run -p 8000:8000 --detach python-fastapi
  • docker run -p 8000:8000 --mount type=bind,source="$(pwd)"/../external.txt,target=/mypath/external.txt,readonly python-fastapi

Move the container to a differnent computer

  • docker save -o
  • docker load -i

Move the image to Goolge Container Registry

  • read the project ID on Google (e.g., google-id)
  • gcloud auth configure-docker
  • docker tag python-fastapi eu.gcr.io/google-id/python-fastapi
  • docker push eu.gcr.io/google-id/python-fastapi
  • created an ad hoc VM mounting the docker image present in the Container Registry

Resources