-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Backend Hot Reload Not Working in Docker Compose Development Environmen #1411
-
First Check
- I added a very descriptive title here.
- I used the GitHub search to find a similar question and didn't find it.
- I searched in the documentation/README.
- I already searched in Google "How to do X" and didn't find any information.
- I already read and followed all the tutorial in the docs/README and didn't find an answer.
Commit to Help
- I commit to help with one of those options 👆
Example Code
#docker-compose.override.yml
#original
services:
# Local services are available on their ports, but also available on:
# http://api.localhost.tiangolo.com: backend
# http://dashboard.localhost.tiangolo.com: frontend
# etc. To enable it, update .env, set:
# DOMAIN=localhost.tiangolo.com
proxy:
image: traefik:3.0
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- "80:80"
- "8090:8080"
# Duplicate the command from docker-compose.yml to add --api.insecure=true
command:
# Enable Docker in Traefik, so that it reads labels from Docker services
- --providers.docker
# Add a constraint to only use services with the label for this stack
- --providers.docker.constraints=Label(`traefik.constraint-label`, `traefik-public`)
# Do not expose all Docker services, only the ones explicitly exposed
- --providers.docker.exposedbydefault=false
# Create an entrypoint "http" listening on port 80
- --entrypoints.http.address=:80
# Create an entrypoint "https" listening on port 443
- --entrypoints.https.address=:443
# Enable the access log, with HTTP requests
- --accesslog
# Enable the Traefik log, for configurations and errors
- --log
# Enable debug logging for local development
- --log.level=DEBUG
# Enable the Dashboard and API
- --api
# Enable the Dashboard and API in insecure mode for local development
- --api.insecure=true
labels:
# Enable Traefik for this service, to make it available in the public network
- traefik.enable=true
- traefik.constraint-label=traefik-public
# Dummy https-redirect middleware that doesn't really redirect, only to
# allow running it locally
- traefik.http.middlewares.https-redirect.contenttype.autodetect=false
networks:
- traefik-public
- default
db:
restart: "no"
ports:
- "5432:5432"
adminer:
restart: "no"
ports:
- "8080:8080"
backend:
restart: "no"
ports:
- "8000:8000"
build:
context: ./backend
# command: sleep infinity # Infinite loop to keep container alive doing nothing
command:
- fastapi
- run
- --reload
- "app/main.py"
develop:
watch:
- path: ./backend
action: sync
target: /app
ignore:
- ./backend/.venv
- .venv
- path: ./backend/pyproject.toml
action: rebuild
# TODO: remove once coverage is done locally
volumes:
- ./backend/htmlcov:/app/htmlcov
environment:
SMTP_HOST: "mailcatcher"
SMTP_PORT: "1025"
SMTP_TLS: "false"
EMAILS_FROM_EMAIL: "noreply@example.com"
mailcatcher:
image: schickling/mailcatcher
ports:
- "1080:1080"
- "1025:1025"
frontend:
restart: "no"
ports:
- "5173:80"
build:
context: ./frontend
args:
- VITE_API_URL=http://localhost:8000
- NODE_ENV=development
playwright:
build:
context: ./frontend
dockerfile: Dockerfile.playwright
args:
- VITE_API_URL=http://backend:8000
- NODE_ENV=production
ipc: host
depends_on:
- backend
- mailcatcher
env_file:
- .env
environment:
- VITE_API_URL=http://backend:8000
- MAILCATCHER_HOST=http://mailcatcher:1080
# For the reports when run locally
- PLAYWRIGHT_HTML_HOST=0.0.0.0
- CI=${CI}
volumes:
- ./frontend/blob-report:/app/blob-report
- ./frontend/test-results:/app/test-results
ports:
- 9323:9323
networks:
traefik-public:
# For local dev, don't expect an external Traefik network
external: false
Description
In the development environment using Docker Compose, changes made to the source code are not automatically updating in the backend container. It is necessary to manually restart the container for the modifications to be applied, which interrupts the development workflow.
Operating System
Linux
Operating System Details
docker compose up -d
Python Version
python:3.10
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions
I hope I understand you correctly, this is my way of development. (I'm a newbie)
- I use uv to install dependencies in the backend folder
uv sync
Start the app:
docker compose watch
Stop the back-end on the docker
docker compose stop backend
get in backend folder on the local environment
cd backend
activate the virtual environment
source .venv/bin/activate
start the FastApi on the local environment and work there, changes immediately effect the docker
fastapi dev app/main.py --reload
Some sources:
development documentation
Replies: 1 comment 1 reply
-
I hope I understand you correctly, this is my way of development. (I'm a newbie)
- I use uv to install dependencies in the backend folder
uv sync
Start the app:
docker compose watch
Stop the back-end on the docker
docker compose stop backend
get in backend folder on the local environment
cd backend
activate the virtual environment
source .venv/bin/activate
start the FastApi on the local environment and work there, changes immediately effect the docker
fastapi dev app/main.py --reload
Some sources:
development documentation
Beta Was this translation helpful? Give feedback.
All reactions
-
just docker compose watch
should also work (I tested and it works).
In current version of the template docker will sync changes and since FastAPI is run inside the container as
command:
- fastapi
- run
- --reload
- "app/main.py"
(with--reload
)
it will restart server on every update
Beta Was this translation helpful? Give feedback.