0
0
Dockerdevops~5 mins

Compose watch for development in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When developing software, you want your app to update automatically as you change the code. Compose watch helps by restarting your app inside Docker whenever you save changes, so you don't have to stop and start it manually.
When you want your web app to reload automatically after editing files during development.
When you need to see changes in your backend service immediately without rebuilding the Docker image each time.
When working with multiple services that depend on each other and want to keep them running while updating code.
When you want to save time by avoiding manual container restarts after code changes.
When you want to test changes quickly in a local Docker environment before deploying.
Config File - docker-compose.yml
docker-compose.yml
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.

Commands
This command starts the services defined in docker-compose.yml. It builds the container if needed, mounts your code, and runs the app in development mode with watching enabled.
Terminal
docker-compose up
Expected OutputExpected
Creating network "default" with the default driver Creating compose-watch-web-1 ... done Attaching to compose-watch-web-1 web_1 | web_1 | > my-app@1.0.0 dev /app web_1 | > nodemon server.js web_1 | web_1 | [nodemon] 2.0.22 web_1 | [nodemon] to restart at any time, enter `rs` web_1 | [nodemon] watching path(s): *.* web_1 | [nodemon] watching extensions: js,mjs,json web_1 | [nodemon] starting `node server.js`
This command shows the status of the running containers to confirm your app is up and running.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports -------------------------------------------------------------------------------- compose-watch-web-1 sh -c npm install && npm run dev Up 0.0.0.0:3000->3000/tcp
This command stops and removes the containers and network created by docker-compose up, cleaning up your environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping compose-watch-web-1 ... done Removing compose-watch-web-1 ... done Removing network default
Key Concept

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.

Common Mistakes
Not mounting the local code directory as a volume in docker-compose.yml
Without the volume mount, the container uses its own copy of the code and won't see your changes, so live reload won't work.
Add a volumes section like './app:/app' to the service in docker-compose.yml to share your code with the container.
Running the app without a development watch command like nodemon or npm run dev
The app won't restart automatically on code changes, so you lose the benefit of live reload.
Use a command that runs the app in watch mode, for example 'nodemon server.js' or 'npm run dev' that includes watching.
Not exposing or mapping the container port to the host
You won't be able to access the app from your browser on your computer.
Add a ports section like '3000:3000' to docker-compose.yml to map container port to your machine.
Summary
Use docker-compose.yml to define your app service with code volume mounts and a watch command.
Run 'docker-compose up' to start the app with live reload enabled inside the container.
Use 'docker-compose ps' to check running containers and 'docker-compose down' to stop and clean up.