lucasvmigotto / python-fastapi

A Python FastAPI template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python with FastAPI template

A template repository with an API developed in Python using FastAPI framework

Pre-requisites

Mandatory

Optional

Development

  1. Clone the repository

    • With HTTP

      # With HTTPS
      git clone https://github.com/LucasVmigotto/python-fastapi.git
    • With SSH

      git clone git@github.com:LucasVmigotto/python-fastapi.git
  2. Open the project with Visual Studio Code:

    code <project folder>

    If the project not already open inside a container, use CTRL + Shift + P and run the Dev Containers: Rebuild Container Without Cache command

  3. Happy coding 😁

Useful Docker commands

Containers

  • List all Docker containers

    docker ps -a
  • Remove Docker Compose containers

    docker compose rm --stop -f
  • Prune containers

    docker container prune --force

Images

  • List all Docker images

    docker ls -a
  • Remove Docker dangling images

    docker image rm -f $(docker image ls --filter "dangling=true" -aq)

    WARNING: If you want to remove ALL Docker images, just remove the --filter flag and argument

    docker image rm -f $(docker image ls -aq)

Volumes

  • List all Docker volumes

    docker volume ls
  • Prune Docker volumes

    docker volume prune --force

Running the application

Inside the Visual Studio Code

  • Run the following command, inside the Visual Studio Code terminal, to start the application:

    poetry run python \
        -m uvicorn \
        --host 0.0.0.0 \
        --port 8001 \
        --log-level debug \
        --reload

    Be careful after running this command with the port in use to not get any conflict using the Docker Compose service

    To make sure, run the application only with the Docker Compose service, and use the Visual Studio Code dev container exclusive as code environment.

Inside a separate container (with Docker Compse)

  • Run the following command to start the container service:

    docker compose up api

    You can use docker compose -d up api to run in detached mode. Although, to be able to see any server logs, you will need to run docker compose logs -f api

    Once successfully started, the service will be available in localhost:8000

Deployment

Build the Docker container for Production environment

  • Run the following command in the first level of the project's folder:

    docker build \
    -f Dockerfile
    --tag python-fastapi \
    --no-cache \
    .

    If necessary, add the --progress plain to see all build output

    • You can test the container with:

      docker run \
          --publish 8000:80 \
          --rm \
          python-fastapi

      Give it a try in localhost:8000

References

About

A Python FastAPI template


Languages

Language:Python 57.8%Language:Dockerfile 42.2%