Developing a microservice using Docker
In this section, we will package the FastAPI service in a standardized way using Docker. This way, we can deploy the Docker image or container on the deployment target of your choice within around 5 minutes.
Docker has several advantages, such as replicability, security, development simplicity, and so on. We can use the official Docker image of fastAPI
(tiangolo/uvicorn-gunicorn-fastapi
) from Docker Hub. Here is a snippet of the Dockerfile:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 COPY ./app /app RUN pip install -r requirements.txt EXPOSE 80 CMD ["uvicorn", "weather_api:app", "--host", "0.0.0.0", "--port", "80"]
Firstly, we use an official fastAPI
Docker image from Docker Hub by using the FROM
command and pointing to the image – tiangolo/uvicorn-gunicorn-fastapi:python3.7
. The image uses Python 3.7, which is compatible with fastAPI
. Next, we copy the app
folder...