Hi,
Can anyone share working docker-compose.yml file for basic FastAPI code.
Preferred if connected to PostgreSQL
Thanks,
Rakesh
You might find the docker-compose.yml file provided in this project useful. It's much simpler than the full-stack setup provided and a bit easier to understand(although it lacks much of the fancier items).
Using that repo, if you wanted the most basic setup, I'd just use the backend and postgres services and you should be good to go!
Hi @IanSaucy
Thanks for your reply. I am quite new to docker if you don't mind can you please paste minimal Dockerfile and docker-compose.yml file. Hopefully I am not asking much
It's hard to post docker files without knowing the structure of your entire project. Docker files do not include your source code(ie all the routes for fastAPI etc), instead they say where and how it should run the code you already have -- thus it needs to factor in your file structure. If you have a repo with your project or something I might be able to help.
I would look up some basic docker and docker-compose info/tutorials. Personally, I found it useful to understand the technology a little before implementing it into a project otherwise you end up copy-pasting without an idea of what is happening.
Maybe one of these tutorial would be helpful? Also, I really think the react or full stack example linked in the previous example would be a good starting point.
https://medium.com/@mike.p.moritz/using-docker-compose-to-deploy-a-lightweight-python-rest-api-with-a-job-queue-37e6072a209b
https://testdriven.io/blog/fastapi-crud/
Good luck!
Thank you @IanSaucy Links look amazing I will definitely check them out. I was trying docker for this minimal fast-api template.
The docker files that I have already tried are in readme file. But couldn't get them working
Hey @rakesh4real,
Here is a version of these files
Dockerfile
FROM python:3.8.1
ENV PYTHONUNBUFFERED 1
EXPOSE 8000
WORKDIR /app
COPY requirements ./
RUN pip install -r requirements.txt
COPY . ./
CMD uvicorn --host=0.0.0.0 app.main:app
docker-compose.yml
version: '3.7'
services:
app:
build: .
ports:
- "8000:8000"
depends_on:
- db
environment:
SQLALCHEMY_DATABASE_URL: "postgres://postgres:test@db/postgres"
db:
image: postgres:9.6
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: test
Hope this helps
Thank you @mickeypash @IanSaucy. Issue resolved closing it. I have updated the repo with files.
Giving the docker file, could you explain how PYTHONUNBUFFERED afects fastapi?
Thanks
By setting the PYTHONUNBUFFERED=1 or TRUE you are telling Python to not buffer the stdout/stderr streams, see docs. In other words, log messages are immediately dumped to the stream, thus receiving them in a timely manner. This means that you can do docker logs <container-id> and see the logs for your FastAPI app as they come in rather than having to wait for them to be flushed from the buffer.
"PYTHONUNBUFFERED also ensures that no partial output is held in a buffer somewhere and never written to terminal logs in case the python application crashes."
That will explain why prints are printed after restarting the server
Thanks!
Thanks for the help here everyone! :clap: :bow:
Thanks for reporting back and closing the issue :+1:
Most helpful comment
Hey @rakesh4real,
Here is a version of these files
Dockerfile
docker-compose.yml
Hope this helps