Docker Compose Watch is a powerful tool for monitoring changes in your Docker Compose setup and automatically rebuilding or restarting services when changes are detected. This lab demonstrates how to implement and use Docker Compose Watch for streamlined development workflows.
This lab explores Docker Compose Watch functionality, which observes changes in project files such as Dockerfile, docker-compose.yaml, or application code, and triggers automated actions like rebuilding images or restarting containers. This is particularly valuable in development environments where frequent code changes occur.
- Docker and Docker Compose installed
- Basic understanding of Docker Compose
- Familiarity with file watching concepts
- Linux/macOS environment (for inotify-tools examples)
Docker Compose Watch is a mechanism that monitors file system changes and automatically triggers container rebuilds or restarts. It eliminates the need for manual intervention when iterating on containerized applications, significantly improving development efficiency.
- Efficiency: Automates rebuilding and restarting processes
- Consistency: Ensures changes are immediately reflected in containers
- Productivity: Allows developers to focus on coding instead of container management
- Reduced Downtime: Minimizes delays through automatic service restarts
- Improved Feedback Loop: Provides immediate reflection of changes
Automatically reloads applications when code changes are detected in source files.
Keeps multiple services synchronized during development cycles.
Applies configuration file changes without requiring manual restarts.
Facilitates quick testing of changes in isolated container environments.
version: '3.8'
services:
app:
build: .
volumes:
- ./app:/usr/src/app
command: npm start
develop:
watch:
- action: rebuild
path: ./package.json
- action: sync
path: ./app
target: /usr/src/appversion: '3.8'
services:
app:
build: .
volumes:
- ./config:/usr/src/app/config
command: npm start
develop:
watch:
- action: restart
path: ./configversion: '3.8'
services:
frontend:
build: ./frontend
develop:
watch:
- action: sync
path: ./frontend/src
target: /app/src
backend:
build: ./backend
develop:
watch:
- action: rebuild
path: ./backend/requirements.txt
- action: sync
path: ./backend/app
target: /app#!/bin/bash
# Initialize the counter file
counter_file="counter.txt"
if [ ! -f "$counter_file" ]; then
echo 0 > "$counter_file"
fi
# Watch for changes in the counter file
while inotifywait -e modify "$counter_file"; do
echo "Change detected in $counter_file. Running Python script..."
python3 print_counter.py
doneimport os
import time
# Specify the counter file
counter_file = 'counter.txt'
# Infinite loop to print the version and sleep for 3 seconds
while True:
# Check if the file exists
if os.path.exists(counter_file):
with open(counter_file, 'r') as file:
counter = int(file.read().strip())
print(f"Current counter value: {counter}")
# Increment the counter and write back to the file
with open(counter_file, 'w') as file:
file.write(str(counter + 1))
else:
print(f"File '{counter_file}' does not exist.")
# Sleep for 3 seconds
time.sleep(3)- Native support in Docker Compose v2.22+
- Uses the
develop.watchconfiguration - Supports
sync,rebuild, andrestartactions
- docker-compose-watch: Dedicated third-party tool
- entr: General-purpose file watcher for triggering Docker commands
- inotify-tools: Linux utilities for monitoring file system changes
- watchman: Facebook's file watching service
- Synchronizes files between host and container
- Fastest option for code changes
- Ideal for interpreted languages
- Rebuilds the Docker image when dependencies change
- Used for package.json, requirements.txt changes
- Maintains container state when possible
- Restarts the entire container
- Used for configuration changes
- Most comprehensive but slowest option
- Basic Setup: Create a simple web application with Docker Compose Watch
- Multi-Action Watch: Configure different watch actions for different file types
- Performance Testing: Compare development speed with and without watch functionality
- Custom Scripting: Implement custom file watching using bash scripts and inotify
- Troubleshooting: Practice debugging watch configuration issues
- Use
syncfor frequently changing source code - Use
rebuildfor dependency files - Use
restartfor configuration changes - Exclude unnecessary files using
.dockerignore - Monitor resource usage during development
- Test watch configuration in team environments
Docker Compose Watch is ideal for:
- Development environments with frequent code changes
- Projects with complex multi-service dependencies
- Teams requiring rapid collaboration and testing
- Applications with mixed programming languages
- Scenarios requiring immediate feedback on changes
This lab includes practical sample files:
docker-compose.yml: Comprehensive example with multiple services and watch configurationsdocker-compose-simple.yml: Simplified version focusing on core watch features from README examplesdemo.sh: Interactive script that creates a sample project structure and provides usage instructions
# Start services with watch enabled
docker compose watch
# Start specific services with watch
docker compose watch [service-name]
# Combine with other commands
docker compose up --watch
# Use the simple example
docker compose -f docker-compose-simple.yml up --watch
# Run the demo script to set up a complete example
./demo.sh# View logs to monitor watch events
docker compose logs -f
# Check watch configuration
docker compose configAfter completing this lab, you will understand:
- How to configure Docker Compose Watch for different scenarios
- The differences between sync, rebuild, and restart actions
- Best practices for optimizing development workflows
- Integration techniques with existing development tools
- Performance considerations and troubleshooting methods
