Hot reload on code changes not consistent on Docker container on Linux

Hello! I’m currently using Docker on two devices with one on Arch and one on MacOS. The Mac version seems to be alright with any code changes automatically triggering Watchfiles to restart and apply the changes. However with Linux, it seems to be inconsistent where it could be working for a bit and sometimes, it won’t. I have to rebuild the entire container to apply these changes.

I have already mounted the entire application to the app directory in the container.

services:
  backend:
    build:
      context: .  # This points to the parent directory of the /dev folder
      dockerfile: Dockerfile
    container_name: backend
    volumes:
      - .:/app

I could not give the entire Dockerfile since I am not sure how sensitive the information is. But based on my other Docker-based projects also, mounting the volume works on having the container refresh on code changes. Both containers are also hosted locally with the drives also being local. Any ideas why this could be happening? Thanks!

Are you using Docker Desktop on Linux as well, or only on macOS and Docker CE on Linux?

Hello! Used to use Docker Desktop on both but found out it was the same case. Tried using Docker engine only and it was still the same issue. What’s weird is that other docker containers that I have on the Linux machine (not running at the same time although smaller) seem to trigger the restart by watchfiles consistently except for this container which is bigger in terms of project size.

I have already tried:

  1. Using watch instead of volume binding on sync+restart
  2. Inotify limits are already on 524288.
  3. Recloning and rebuilding the container multiple times.

EDIT: For more context, all projects (the smaller and bigger one) are FastAPI projects and already have the reload flag on uvicorn.

Since inotify would have been my next idea, I think I have none. I don’t work with projects currently that need to reload automatically so I don’t experience such issues.

Does it work when running outside containers, directly on the host?

I think I may have found a light for this. I checked my docker logs deeper and found this.

backend   | INFO:     Started reloader process [16] using WatchFiles
backend   | Traceback (most recent call last):
backend   |   File "/usr/local/bin/uvicorn", line 8, in <module>
backend   |     sys.exit(main())
backend   |              ^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
backend   |     return self.main(*args, **kwargs)
backend   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1078, in main
backend   |     rv = self.invoke(ctx)
backend   |          ^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
backend   |     return ctx.invoke(self.callback, **ctx.params)
backend   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
backend   |     return __callback(*args, **kwargs)
backend   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 418, in main
backend   |     run(
backend   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 582, in run
backend   |     ChangeReload(config, target=server.run, sockets=[sock]).run()
backend   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 52, in run
backend   |     for changes in self:
backend   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 71, in __next__
backend   |     return self.should_restart()
backend   |            ^^^^^^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/uvicorn/supervisors/watchfilesreload.py", line 92, in should_restart
backend   |     changes = next(self.watcher)
backend   |               ^^^^^^^^^^^^^^^^^^
backend   |   File "/usr/local/lib/python3.11/site-packages/watchfiles/main.py", line 118, in watch
backend   |     with RustNotify(
backend   |          ^^^^^^^^^^^
backend   | PermissionError: Permission denied (os error 13) about ["/app/ci_cd/developer/postgres_data"]

Maybe this is an issue with watchfiles itself. I already chowned it though so I’m a bit confused as to why this persists.

I seem to have fixed the error by setting user: "0:0" on the docker compose file. I know this basically grants the container root user permissions. Would this cause any issues?

I say it often, but it depends on the application. It is not good in terms of security, but you know that.

You could try Docker CE as rootless Docker (running as your user). Then you can edit it, but your user would be root in the container and the root in the container would not be root on the host.

1 Like

Thank you so much! I learned a lot today. I’ll definitely look into the cons of setting this in compose but for now, I’m just happy I’m able to edit my files and have it reflect immediately. Have a great day!