r/FastAPI • u/namesry • Apr 03 '21
Question Docker deployment not reloading, what am I doing wrong?
Trying to deploy a simple FastAPI web app with Docker but running into an issue when deployed through Docker where my scripts either aren't being called or the endpoint just isn't updating, not sure which.
Aside from the essential imports & instantiation of FastAPI object, my main.py looks like this:
@app.get('/status')
async def status():
available = checkAvailable() # imported method, calls an external API
if available:
return {"status" : "free"}
else:
return {"status" : "busy"}
if __name__ == '__main__:
uvicorn.run('app.main:app', port=80, host='0.0.0.0', reload=True)When I deploy uvicorn locally everything works as expected. Changing availability in the external API & then refreshing the FastAPI properly changes the status.
When deployed via Docker though, the status at the point of the docker image being created is maintained no matter any refresh. My Dockerfile is as follows:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app/
COPY requirements.txt .
RUN pip3 --no-cache-dir install -r requirements.txt
COPY . /app/
CMD ["python3", "./app/main.py"]I've google around and couldn't find anyone talking about having this issue and I really just don't understand if my error is my understanding of FastAPI or Docker, or both.
Any and all help is appreciated. I know there's lots to improve with the app but I'm just starting to learn and want to have a successful deployment of a simple app before digging deeper.
2
u/double07zip Apr 03 '21
I think your issue is due to copying the code into /app in the container. This is different from mounting your local directory which should achieve your expected behaviour.
1
u/magicaldelicious Apr 03 '21
It's pretty simple. Your CMD is the only thing the container cares is running. When that stops / reloads the container is gone. And, in your case, it appears to "stop".
There's documentation to show you how to start the container correctly when you want to leverage debug:
https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#development-live-reload
You could also just use ENTRYPOINT in your Dockerfile. This is handy because you can just add a script to your container image called entrypoint.sh, for example, and as long as that script is running your container will stay alive. So if Gunicorn restarts your container won't care because the parent process is still running.
To use ENTRYPOINT write a shell script that calls what's in your CMD as another option. Also keep in mind that the way most people use it is that ENTRYPOINT consumes CMD unless overridden. Meaning that my ENTRYPOINT calls entrypoint.sh and if I run my container as normal the CMD baked into it will be passed as a command line option to the script. And I pull that out of my script using something like exec $@.
Others have answered this much more completely than I did here, on mobile so apologies. But hopefully this gets you headed towards a solution:
And here's a simple working example as part of a project I was working on:
https://gitlab.com/splatops/cntn-speedtest/-/tree/master/speedtest
The project has a few containers but this one in particular shows how a very simple ENTRYPOINT and CMD work together. It's not a FastAPI example but it is a Python utility. That doesn't really matter though.
Anyway, good luck!
1
u/Chains0 Aug 08 '21
How are you starting the container? What request are you sending? What do you receive as a response? Any caches involved?
2
u/[deleted] Apr 03 '21
Without knowing more about your docker deployment setup, i'm not sure if i can help you very much (e.g are you deploying to AWS lambda? k8s cluster? etc), but
make sure you're rebuilding the docker image every time after you make a change and before you deploy the image
you can try running the docker image by building it locally with a tag and then running it (like so)
and then your app will be accessible at
localhost:8000