UVICORN reload inside docker container not working

31 views Asked by At

After trying almost all different solutions on the web I still have issues with hot reloading of a FAST api app inside a Docker container.

I can see that the volume mapping is doing it's thing and the files are updated inside the docker container.

The console log tells me uvicorn is watching files inside the correct folder. But any change i make is not triggering a reload of the app.

I have the following setup:

My docker compose:

#docker-compose.yml
version: "3"

services:
  app:
    container_name: api
    image: api:latest
    depends_on:
      - mysqldb
    build: 
      context: .
      dockerfile: Dockerfile.dev
      args:
        DEV: "true"
    ports:
      - "8080:8080"
    env_file:
      - .env
    environment:
      - WATCHFILES_FORCE_POLLING=true
      - DEV=1
      - MYSQL_HOSTNAME=mysqldb
      - MYSQL_PORT=3306
    command: uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload --reload-dir /src/app
    volumes:
      - ./app:/src/app
  mysqldb:
    container_name: mysqldb
    image: mysql:latest
    restart: always
    ports:
     - 3307:3306
    environment:
      MYSQL_ROOT_PASSWORD: randompassword
      MYSQL_DATABASE: my-database

And my docker compose

#dockerfile.dev
FROM python:3.10-slim-buster

ENV PYTHONUNBUFFERED 1

COPY requirements.txt /
COPY alembic.ini /
COPY migrations /migrations

RUN pip install --upgrade pip setuptools wheel gunicorn uvloop httptools
RUN pip install -r requirements.txt
RUN pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
RUN pip install sentence-transformers 

# Copy the source code to src folder
COPY . /src

ENV PYTHONPATH=/src

EXPOSE 8080

# FOR local dev the startup happens inside the docker-compose with reload options
COPY startup.sh /src/startup.sh
RUN chmod +x /src/startup.sh

CMD ["uvicorn", "app.main:app","--host", "0.0.0.0", "--port", "8080", "--reload"]

I've tried almost all possible configurations of the reload.

  • Without running the uvicorn command inside the docker file
  • With the reload dir as root (/), app, and src/app
  • Without the reload dir
  • With the guvicorn server instead of the uvicorn server

So i'm kinda stuck. Can anyone help me out?

0

There are 0 answers