0
0
Dockerdevops~10 mins

Compose watch for development in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Compose watch for development
Start docker compose watch
Run service containers
Watch source files for changes
Detect file change?
NoKeep running
Yes
Restart affected containers
Continue watching
Docker Compose starts containers, watches source files, and restarts containers automatically when files change.
Execution Sample
Docker
version: '3.8'
services:
  app:
    build: .
    volumes:
      - .:/app
    command: sh -c "while true; do sleep 1; done"
This docker-compose.yml is used with `docker compose watch`. It runs a container with the current folder mounted into /app, keeping it alive with a sleep loop while watch handles restarts on changes.
Process Table
StepActionFile Change DetectedContainer StateOutput
1Start docker compose watchNoContainer startingBuilding image and starting container
2Container runningNoContainer runningApp running, waiting for changes
3File changed in source folderYesContainer restartingStopping container and restarting
4Container restartedNoContainer runningContainer restarted with new code
5No further changesNoContainer runningWaiting for changes
6User stops docker compose watchNoContainer stoppedContainers stopped and removed
💡 User stops docker compose watch, ending watch and container lifecycle
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Container StateNot runningRunningRestartingRunningStopped
File Change DetectedNoNoYesNoNo
Key Moments - 2 Insights
Why does the container restart when a file changes?
Because docker-compose watch detects the file change (see Step 3 in execution_table) and triggers a container restart to apply updates.
What keeps the container running when no file changes happen?
The container runs a command that keeps it alive (like a sleep loop), so it stays running waiting for changes (see Step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the container state at Step 3?
ARunning
BStopped
CRestarting
DStarting
💡 Hint
Check the 'Container State' column at Step 3 in the execution_table.
At which step does the container restart due to file changes?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for 'File Change Detected' marked 'Yes' in the execution_table.
If no file changes occur, what will the container state be after Step 2?
ARestarting
BRunning
CStopped
DStarting
💡 Hint
Refer to 'Container State' at Step 2 in the execution_table.
Concept Snapshot
Docker Compose watch runs containers and monitors source files.
When files change, it restarts containers to update code.
Use volumes to mount source code for live updates.
Keep containers alive with a command like a sleep loop.
Stop with Ctrl+C to end watching and containers.
Full Transcript
Docker Compose watch for development means running your app in containers that automatically restart when you change your code files. First, docker compose watch starts your containers and mounts your source code folder inside them. The containers run a command that keeps them alive, waiting for changes. When you edit a file, docker compose watch detects this change and restarts the affected containers so your app updates with the new code. If no changes happen, the containers keep running. When you want to stop, you press Ctrl+C, and docker compose watch stops and removes the containers. This process helps developers see their code changes live without manually restarting containers.