How to create a real-time web application using FastAPI, Angular and MongoDB
Part 4: Local deployment — Docker

Hi there! My name is Job and in this series I’m going to guide you through my journey of creating a real-time web application. In this multipart guide I tackle all aspects and learnings of creating a real-time web application. In this fourth part I set up the local deployment of our API created in Part 1, the MongoDB database from Part 2, and the Angular frontend from Part 3 using Docker (and just one command!).
Getting setup⚙️
In Part 2 we created the MongoDB database using Docker so this time we don’t have to install anything to get started. Let’s open the API project in PyCharm and create a new folder docker/ with the following files.
api/
|-- docker/
| |-- docker-entrypoint.sh
| |-- Dockerfile
| |-- gunicorn_conf.py
|
|-- src/...
|
|-- pyproject.toml
|-- poetry.lockNext, let’s open the frontend project in VSCode and create a folder docker/ with a Dockerfile and nginx.conf file.
frontend/
|-- docker/
| |-- Dockerfile
| |-- nginx.conf
|
|-- node_modules/...
|-- src/...
|-- ...Getting started🚀
API
I always start with a default docker-entrypoint.sh which is going to serve as a mechanism for passing optional parameters to the Docker container. It will also start the Python environment managed by Poetry.
Then we need a Dockerfile that defines our container's image (a blueprint if you will). After looking around for the use case of getting a poetry-based environment running in a Docker container serving FastAPI, I stumbled upon this Github discussion. I found the proposed Dockerfile quite interesting as it uses multi-stage builds for different parts of the Docker container. This enables you to maintain different parts separately. I decided to use it and adjust it according to my needs, so full credit goes to Michael Oliverx.
Pay attention to the lines shown below. The first ENV statements set some environment variables for use later in our API to determine what debug level to use, for example. Next, you see the COPY statement for copying the Gunicorn configuration file. The following ENV statements are used for defining which port the API is going to be listening to and to determine if we’re running in Docker which will come in handy later on.
...
ENV APP_ENV="DOCKER"
ENV TZ=Europe/Amsterdam
ENV PYTHONPATH "/app"
ENV LOG_LEVEL "debug"
...
COPY ./docker/gunicorn_conf.py /gunicorn_conf.py
...
ENV PORT=8000






