Use Compose Watch
Use Compose Watch
https://docs.docker.com/compose/how-tos/file-watch/#:~:text=Run%20docker%20compose%20up%20--watch%20to%20build%20and,source%20files%20using%20your%20preferred%20IDE%20or%20editor.
The
watch
attribute automatically updates and previews your running Compose services as you edit and save your code. For many projects, this enables a hands-off development workflow once Compose is running, as services automatically update themselves when you save your work.
watch
adheres to the following file path rules:
- All paths are relative to the project directory
- Directories are watched recursively
- Glob patterns aren't supported
- Rules from
.dockerignore
apply
- Use
ignore
option to define additional paths to be ignored (same syntax)- Temporary/backup files for common IDEs (Vim, Emacs, JetBrains, & more) are ignored automatically
.git
directories are ignored automaticallyYou don't need to switch on
watch
for all services in a Compose project. In some instances, only part of the project, for example the Javascript frontend, might be suitable for automatic updates.Compose Watch is designed to work with services built from local source code using the
build
attribute. It doesn't track changes for services that rely on pre-built images specified by theimage
attribute.
作为APP镜像,应该将程序可运行文件打包进入docker镜像, 除了配置部分,这样便于应用部署。
# Run as a non-privileged user FROM node:18 RUN useradd -ms /bin/sh -u 1001 app USER app # Install dependencies WORKDIR /app COPY package.json package-lock.json ./ RUN npm install # Copy source files into application directory COPY --chown=app:app . /app
但是对于开发环境, 需要将本地代码及时更新到容器内, compose watch提供开发环境支持。
例如此例子fastapi官方项目模板:
https://github.com/fastapi/full-stack-fastapi-template/blob/master/docker-compose.override.yml
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2021-01-11 Visualizing the stock market structure of sklearn
2017-01-11 lua 立即执行函数
2015-01-11 一种构造WEB服务器端recv和send接口阻塞现象的方法