[PYTHON] Build a lightweight Fast API development environment using Docker

Introduction

Recently, I often use Fast API, and I wanted to build it quickly with Docker and throw it away. As a memo, I would like to leave a way to build it lightweight.

Preparation

--Environment where Docker and Docker Compose can be used

version


$ docker --version
Docker version 19.03.8, build afacb8b7f0

--The file structure looks like this

file organization


.
├── Dockerfile
├── docker-compose.yml
├── main.py
└── requirements.txt

Source

You can clone from here https://github.com/sattosan/sample-fastapi-alpine

Dockerfile Made based on alpine

Dockerfile


FROM python:3.8-alpine

WORKDIR /app

ADD requirements.txt .
#Install the required packages inside the container
RUN apk add --no-cache build-base mariadb-connector-c-dev
# requirements.Install the Python packages listed in txt
RUN pip install --no-cache-dir --trusted-host pypi.python.org -r requirements.txt

ADD main.py .
#Wait for FastAPI on port 8000
CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"]

Docker Compose

docker-compose.yml


version: "3.0"

services:
  # FastAPI
  api:
    container_name: "api"
    build: .
    restart: always
    tty: true
    ports:
      - 8000:8000

We have put together the necessary packages

requirements.txt


uvicorn
fastapi

Sample code

I have prepared the sample code in the FastAPI documentation (https://fastapi.tiangolo.com/).

main.py


from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

Run

$ docker-compose up -d --build

Check Docker Image size

--Alpine-based Image size

docker images | grep api
latest              bff0158b61ca        29 minutes ago      275MB

--Image size of the head family

It is the result of building with reference to Honke

$ docker images | grep api
latest              878110f2207f        11 seconds ago      1.02GB

You can see that it was reduced by 3.6 times compared to the original family

Operation check

APIs generated by FastAPI are automatically documented by Swagger

Take a look at Swagger

You can see the documentation by accessing domain / docs

http://localhost:8000/docs

キャプチャ.PNG

Try to hit the API

If you hit it with curl, it will come back like this

result


$ curl localhost:8000
{"Hello":"World"}

in conclusion

This time, I tried to build a lightweight development environment based on alpine.

It is 3.6 times lighter than the original house, so I think it is a sufficient environment for disposable items. Recently, the Fast API has begun to be used in various projects including Netflix, so If you haven't used it yet, please try it!

--Netflix OSS: https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072

Reference site

--Honke: https://fastapi.tiangolo.com/

Recommended Posts

Build a lightweight Fast API development environment using Docker
Build a go environment using Docker
Build a Flask development environment at low cost using Docker
Build a Fast API environment with docker-compose
[Python] Build a Django development environment with Docker
Build a Django development environment using pyenv-virtualenv on Mac
Build a Python development environment using pyenv on MacOS
Build a development environment with Poetry Django Docker Pycharm
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Memo] Build a development environment for Django + Nuxt.js with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Easily build a development environment with Laragon
Build a local development environment for Lambda + Python using Serverless Framework
[Linux] Build a jenkins environment with Docker
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a LAMP environment on your local Docker
Build a C language development environment with a container
Build Django + NGINX + PostgreSQL development environment with Docker
Go (Echo) Go Modules × Build development environment with Docker
Build a Python development environment on your Mac
Build a Django development environment with Doker Toolbox
Build a Kubernetes environment for development on Ubuntu
Build a Minecraft plugin development environment in Eclipse
Build a mruby development environment for ESP32 (Linux)
Build a Python development environment on Raspberry Pi
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Build a local development environment for Laravel6.X on Mac
Build a GVim-based Python development environment on Windows 10 (1) Installation
How to build a Django (python) environment on docker
I want to easily build a model-based development environment
Build a Python development environment on Mac OS X
[Go + Gin] I tried to build a Docker environment
Build a Python environment on your Mac using pyenv
Build a machine learning application development environment with Python
How to build a development environment for TensorFlow (1.0.0) (Mac)
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Build a LAMP environment [CentOS 7]
[ev3dev × Python] Build ev3dev development environment
Build a machine learning environment
Build a Python environment offline
Build a Docker environment that can use PyTorch and JupyterLab
Building a Jupyter Lab development environment on WSL2 using Anaconda3
Build a seq2seq model using keras's Functional API Model building & learning
Build a web API server at explosive speed using hug
Build a Python development environment in Eclipse (add HTML editor)
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
Build a comfortable development environment with VSCode x Remote Development x Pipenv
How to build a python2.7 series development environment with Vagrant
Create a Todo app with Django ① Build an environment with Docker
Until building a Python development environment using pyenv on Ubuntu 20.04
Generate a Docker image using Fabric
How to build a LAMP environment using Vagrant and VirtulBox Note
Build a Chainer environment using CUDA and cuDNN on a p2 instance
Build a deb file with Docker
Build Mysql + Python environment with docker
Try using virtualenv, which can build a virtual environment for Python
The simplest way to build a Spleeter usage environment using Windows
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS