---
description: Use File watch to automatically update running services as you work
keywords: compose, file watch, experimental
title: Use Compose Watch
weight: 50
aliases:
- /compose/file-watch/
---
{{< summary-bar feature_name="Compose file watch" >}}
{{% include "compose/watch.md" %}}
`watch` adheres to the following file path rules:
* All paths are relative to the project directory, apart from ignore file patterns
* 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 automatically
You 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 the `image` attribute.
## Compose Watch versus bind mounts
Compose supports sharing a host directory inside service containers. Watch mode does not replace this functionality but exists as a companion specifically suited to developing in containers.
More importantly, `watch` allows for greater granularity than is practical with a bind mount. Watch rules let you ignore specific files or entire directories within the watched tree.
For example, in a JavaScript project, ignoring the `node_modules/` directory has two benefits:
* Performance. File trees with many small files can cause a high I/O load in some configurations
* Multi-platform. Compiled artifacts cannot be shared if the host OS or architecture is different from the container
For example, in a Node.js project, it's not recommended to sync the `node_modules/` directory. Even though JavaScript is interpreted, `npm` packages can contain native code that is not portable across platforms.
## Configuration
The `watch` attribute defines a list of rules that control automatic service updates based on local file changes.
Each rule requires a `path` pattern and `action` to take when a modification is detected. There are two possible actions for `watch` and depending on
the `action`, additional fields might be accepted or required.
Watch mode can be used with many different languages and frameworks.
The specific paths and rules will vary from project to project, but the concepts remain the same.
### Prerequisites
In order to work properly, `watch` relies on common executables. Make sure your service image contains the following binaries:
* stat
* mkdir
* rmdir
`watch` also requires that the container's `USER` can write to the target path so it can update files. A common pattern is for
initial content to be copied into the container using the `COPY` instruction in a Dockerfile. To ensure such files are owned
by the configured user, use the `COPY --chown` flag:
```dockerfile
# 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
```
### `action`
#### Sync
If `action` is set to `sync`, Compose makes sure any changes made to files on your host automatically match with the corresponding files within the service container.
`sync` is ideal for frameworks that support "Hot Reload" or equivalent functionality.
More generally, `sync` rules can be used in place of bind mounts for many development use cases.
#### Rebuild
If `action` is set to `rebuild`, Compose automatically builds a new image with BuildKit and replaces the running service container.
The behavior is the same as running `docker compose up --build <svc>`.
Rebuild is ideal for compiled languages or as a fallback for modifications to particular files that require a full