Compose watch for development in Docker - Commands & Configuration
version: '3.8' services: web: image: node:18 working_dir: /app volumes: - ./app:/app command: sh -c "npm install && npm run dev" ports: - "3000:3000"
This file defines a service named web that uses the official Node.js 18 image.
The working_dir sets the working directory inside the container to /app.
The volumes line mounts your local ./app folder into the container's /app folder, so code changes are visible inside the container.
The command runs npm install to install dependencies, then npm run dev to start the app in development mode with watching enabled.
The ports line maps port 3000 inside the container to port 3000 on your computer, so you can access the app in your browser.
docker-compose up
docker-compose psdocker-compose down
If you remember nothing else from this pattern, remember: mounting your code as a volume lets the container see your changes instantly, enabling live reload during development.
Practice
docker-compose watch during development?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 CQuick Check:
Compose watch = auto reload on code change [OK]
- Confusing watch with building images
- Thinking watch deploys to production
- Assuming watch monitors resource usage
docker-compose.yml volume configurations correctly syncs your local source code to the container for live reload?Solution
Step 1: Understand volume syntax in docker-compose
Volumes map host paths to container paths usinghost_path:container_path.Step 2: Identify correct host to container mapping
./app:/appmaps local folderappto container's/appfor syncing code.Final Answer:
volumes:\n - ./app:/app -> Option AQuick Check:
Host path before colon, container path after [OK]
- Reversing host and container paths
- Omitting container path
- Using absolute container path as host path
docker-compose.yml snippet and a Node.js app using nodemon:
services:
web:
volumes:
- ./src:/usr/src/app
command: nodemon index.js
What happens when you edit a file inside ./src on your host machine?Solution
Step 1: Understand volume syncing
The volume syncs./srcon host to/usr/src/appin container, so changes reflect inside container.Step 2: Understand nodemon behavior
nodemonwatches files and restarts the app when files change.Final Answer:
The container restarts the Node.js app automatically -> Option DQuick Check:
Volume sync + nodemon = auto restart on code change [OK]
- Thinking image rebuild is needed for code changes
- Assuming container crashes on volume use
- Believing files are deleted on sync
docker-compose with volumes and watchmedo to reload a Python app, but changes don't trigger reload. What is the most likely cause?Solution
Step 1: Check volume configuration
If volumes don't map host code to container, changes won't appear inside container.Step 2: Understand watchmedo needs code inside container
Without synced code, watchmedo cannot detect changes to reload app.Final Answer:
The volume path is incorrect or missing -> Option AQuick Check:
Missing volume = no code sync = no reload [OK]
- Blaming CMD instead of volumes
- Assuming network affects file watching
- Thinking image cache affects reload
docker-compose that automatically reloads a React app on code changes. Which combination below is the best practice?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 startwithreact-scriptsruns dev server that reloads on changes.Final Answer:
Use volumes to sync source code, runnpm startwithreact-scriptswatch mode inside container -> Option BQuick Check:
Volumes + watch mode = best dev reload setup [OK]
- Baking code into image prevents live reload
- Disabling hot reload loses dev speed
- Running app only on host misses container benefits
Ready to prove it?
Pass the test and this pattern moves unknown
