Docker compose --watch keeps cycling FastAPI app on sync+restart

I’m setting up a FastAPI app, and my config for the backend for my docker compose is:

WORKDIR /app
COPY appinfo ./appinfo
COPY app.py README.md ./

The relevant part of my compose file is:

services:
  api:
    build: .
    develop:
      watch:
        - action: sync+restart
          path: ./appinfo
          target: /app
        - action: sync+restart
          path: ./app.py
          target: /app/app.py
        - action: rebuild
          path: pyproject.toml

The problem is every time I make a change the container syncs and then tries to restart, failing with a ModuleNotFoundError: No module named 'apidata.api' error over and over again. I’d assume the file got copied into the wrong place, but I can’t exec into the container to check, as it’s always restarting.

What should the configuration be, or how can I debug what’s going on?

If appinfo is a folder and not a file, I think it syncs the content of ./appinfo to /app and not /app/appinfo. Try this

      watch:
        - action: sync+restart
          path: ./appinfo
          target: /app/appinfo

even in that case you can stop the container and use docker cp to copy files out from the stopped container. Or on a Linux host using Docker CE, you can use docker container inspect to find the “merged” dir of the overlay filesystem and browse the files there.

It wasn’t the ./appinfo path causing the problem — even when I changed it to target /app/appinfo or commented it out completely I still saw the problem — but by using docker cp I was able to verify the app file was being overwritten successfully. It looks like there was some odd misconfiguration in the app which was triggered by the copy/restart and by fixing it I was able to resolve the problem.