[PYTHON] Build a Fast API environment with docker-compose

FastAPI?

It's a modern and fast Python web framework. It seems to be popular recently, so I adopted it for business.

Official documentation https://github.com/tiangolo/fastapi

The environment was different depending on the members and the introduction could not proceed smoothly, It's a memo because everyone can now share the same working environment using Docker.

Created file

We will proceed according to the official procedure.

It is OK if the file structure is like this in the end. Super simple. スクリーンショット 2020-03-30 16.30.06.png

Dockerfile

Install the modules listed in the Installation section of the official documentation. It runs on its own when built, so it's convenient because you don't have to manually search for the required module.

Dockerfile


FROM python:3.7
WORKDIR /var/www/html
RUN pip install fastapi uvicorn

main.py

This is also written with reference to the item of Example. It's an API that returns Hello World objects when the root is accessed.

main.py


from fastapi import FastAPI

app = FastAPI()

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

docker-compose.yaml

It acts as a bridge between Dockerfile and main.py.

After installing the Python module using the Dockerfile Refer to the Run it item and hit the command to start the Fast API server.

Here, it is set to start on port 9004.

docker-compose.yaml


version: '3'

services:
  app:
    container_name: FastAPI
    build: ./docker
    volumes:
      - ./src:/var/www/html
    ports:
      - "9004:9004"
    command: uvicorn main:app --reload --host 0.0.0.0 --port 9004

Operation check

Start and build the container with docker-compose.

$ docker-compose up --build

Creating network "docker_fastapi_default" with the default driver
Building app
Step 1/3 : FROM python:3.7
 ---> 879165535a54
Step 2/3 : WORKDIR /var/www/html
 ---> Using cache
 ---> 31d5c58e6177
Step 3/3 : RUN pip install fastapi uvicorn
 ---> Using cache
 ---> 430430eecf7f
Successfully built 430430eecf7f
Successfully tagged docker_fastapi_app:latest
Creating FastAPI ... done
Attaching to FastAPI
FastAPI | INFO:     Uvicorn running on http://0.0.0.0:9004 (Press CTRL+C to quit)
FastAPI | INFO:     Started reloader process [1]
FastAPI | INFO:     Started server process [7]
FastAPI | INFO:     Waiting for application startup.
FastAPI | INFO:     Application startup complete.

The required modules have been installed and the Fast API server has started.

Let's access it with the curl command.


$ curl http://localhost:9004

{"Hello":"World"}

Reaction as defined in main.py!

Summary

You can easily set up an API server using FastAPI! It's nice to be able to share the same environment with fewer files using Docker: sparkles:

Since you can also execute shell scripts in Python, It is convenient to implement commands for automatic deployment and do something like "pseudo Jenkins". ~~ Don't say you should use Jenkins ~~

Reference URL

Recommended Posts

Build a Fast API environment with docker-compose
Build a lightweight Fast API development environment using Docker
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Easily build a development environment with Laragon
Build a Tensorflow environment with Raspberry Pi [2020]
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Python environment with docker-compose
Build a WardPress environment on AWS with pulumi
Build the fastest Django development environment with docker-compose
Create a python3 build environment with Sublime Text3
Build a Django environment with Vagrant in 5 minutes
[Memo] Build a virtual environment with Pyenv + anaconda
Build a virtual environment with pyenv and venv
Build a Django development environment with Doker Toolbox
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python environment for each directory with pyenv-virtualenv
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a machine learning environment
Build a Python environment offline
Build a development environment with Poetry Django Docker Pycharm
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django environment for Win10 (with virtual space)
Build a numerical calculation environment with pyenv and miniconda3
Build a data analysis environment with Kedro + MLflow + Github Actions
Build Azure Pipelies with Azure DevOps in a Linux self-hosted environment
[Memo] Build a development environment for Django + Nuxt.js with Docker
Build a Go development environment with VS Code's Remote Containers
(Now) Build a GPU Deep Learning environment with GeForce GTX 960
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Build a comfortable development environment with VSCode x Remote Development x Pipenv
Build a speed of light web API server with Falcon
How to build a python2.7 series development environment with Vagrant
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Create a Todo app with Django ① Build an environment with Docker
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
[Study memo] Build GeoJSON vector tile server with Fast API
Building a kubernetes environment with ansible 2
Build FastAPI + uvicorn + nginx with docker-compose
Build a go environment using Docker
Build a deb file with Docker
Build Mysql + Python environment with docker
Create a virtual environment with Python!
Building a virtual environment with Python 3
Build Flask environment with Dockerfile + docker-compose.yml
Build PyPy execution environment with Docker
Build a web application with Django
Build IPython Notebook environment with boot2docker
Build a python3 environment on CentOS7
Building a kubernetes environment with ansible 1