📖 Scenario: You are building a simple development environment using Docker Compose. You want to automatically restart your application container whenever you change the source code files. This helps you see your changes immediately without manually restarting the container.
🎯 Goal: Create a Docker Compose setup that watches for file changes in your app directory and restarts the container automatically during development.
📋 What You'll Learn
Create a Docker Compose file with a service named app
Mount the local ./app directory into the container
Use the restart: always policy initially
Add a command to run a simple watch script inside the container
Print a message showing the container restarts on file changes
💡 Why This Matters
🌍 Real World
Developers often want their code changes to reflect immediately without restarting containers manually. This setup helps automate that during development.
💼 Career
Knowing how to configure Docker Compose for live development is a valuable skill for DevOps engineers and developers working with containerized applications.
Progress0 / 4 steps
1
Create the initial Docker Compose file
Create a file named docker-compose.yml with a service called app that uses the image alpine. Mount the local directory ./app to /app inside the container.
Docker
Hint
Use volumes to mount the local folder ./app into the container path /app.
2
Add restart policy and working directory
In the app service, add restart: always and set working_dir to /app.
Docker
Hint
The restart key controls container restart behavior. working_dir sets the default directory inside the container.
3
Add a command to watch for file changes
Add a command to the app service that runs sh -c "while true; do inotifywait -e modify,create,delete -r /app && echo 'Files changed, restarting...'; sleep 1; done". This will watch for file changes in /app and print a message.
Docker
Hint
Use inotifywait inside a shell loop to watch for file changes and print a message.
4
Print a message when container starts
Add a command that first prints "Starting watch on /app" then runs the watch loop. Use sh -c with echo followed by the while true loop.
Docker
Hint
Chain commands in sh -c using semicolon ; to print a message before the loop.
Practice
(1/5)
1. What is the main purpose of using docker-compose watch during development?
easy
A. To deploy the app to production servers
B. To build the Docker image only once
C. To automatically reload the application when code changes
D. To monitor container resource usage
Solution
Step 1: Understand the role of watch in development
Watching means tracking file changes to trigger actions automatically.
Step 2: Connect watch with docker-compose usage
Compose watch reloads the app inside the container when source code changes on the host.
Final Answer:
To automatically reload the application when code changes -> Option C
Quick Check:
Compose watch = auto reload on code change [OK]
Hint: Watch means auto reload on code change [OK]
Common Mistakes:
Confusing watch with building images
Thinking watch deploys to production
Assuming watch monitors resource usage
2. Which of the following docker-compose.yml volume configurations correctly syncs your local source code to the container for live reload?
easy
A. volumes:\n - ./app:/app
B. volumes:\n - /app:/app
C. volumes:\n - ./app
D. volumes:\n - /app
Solution
Step 1: Understand volume syntax in docker-compose
Volumes map host paths to container paths using host_path:container_path.
Step 2: Identify correct host to container mapping
./app:/app maps local folder app to container's /app for syncing code.
Without synced code, watchmedo cannot detect changes to reload app.
Final Answer:
The volume path is incorrect or missing -> Option A
Quick Check:
Missing volume = no code sync = no reload [OK]
Hint: Check volumes if reload doesn't work [OK]
Common Mistakes:
Blaming CMD instead of volumes
Assuming network affects file watching
Thinking image cache affects reload
5. You want to set up a development environment with docker-compose that automatically reloads a React app on code changes. Which combination below is the best practice?
hard
A. Build image with source code baked in, run npm run build once
B. Use volumes to sync source code, run npm start with react-scripts watch mode inside container
C. Use volumes but disable hot reload to avoid conflicts
D. Run npm start on host and use container only for database
Solution
Step 1: Sync source code with volumes
Volumes keep container code updated with host edits for live reload.
Step 2: Use React's built-in watch mode
npm start with react-scripts runs dev server that reloads on changes.
Final Answer:
Use volumes to sync source code, run npm start with react-scripts watch mode inside container -> Option B
Quick Check:
Volumes + watch mode = best dev reload setup [OK]
Hint: Sync code + run dev server with watch mode [OK]
Common Mistakes:
Baking code into image prevents live reload
Disabling hot reload loses dev speed
Running app only on host misses container benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.