Sub topic 4
Preparing the Dockerfile and build section
This chapter assumes you can write a basic Dockerfile and a Compose file with services, build, and ports. You should also know what an image layer is. Compose Watch only works on services that are set up the way this chapter describes.
Why the build section matters
Compose Watch acts on services that Compose builds from your source. The sync and rebuild actions both depend on that build. A rebuild runs the image build again. A sync copies files into a container whose image already holds your project layout.
A service that uses only image: has nothing to rebuild, so Compose Watch cannot track it. Give the service a build section.
A Dockerfile that works well with watch
Order the instructions so that slow, rarely changing steps come first. Docker caches each layer and reuses it until an earlier layer changes. If you copy the dependency manifest first and install dependencies before copying the rest of the source, a source-only change does not repeat the install.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]The file does four things that matter for watch:
WORKDIR /appfixes where the project lives. Thetargetin your watch rules points inside this directory.COPY package*.json ./andRUN npm installform their own layer. Arebuildtriggered bypackage.jsonreuses cached layers up to this point unless the manifest changed. Here it did change, so the install runs again, as it should.COPY . .comes after the install. Editing a source file invalidates only this layer and the ones after it.CMDstarts the application. Watch changes files in the container, but your process still has to pick them up. That is why a later chapter covers restarts.
The image layout is the contract between your Dockerfile and your watch rules. If the Dockerfile places the code in /app, the rules must target /app/....
The Compose service
Next, add the build section to the service. The develop.watch rules come later, so keep this minimal for now.
services:
web:
build: .
ports:
- "3000:3000"build: . tells Compose to build from the Dockerfile in the current directory. The build context is the directory Docker sends to the builder, and it is also the base for relative watch paths. A rule such as path: ./src is resolved against the project directory on the host.
Common errors
- Using
image:withoutbuild:. Compose has nothing to rebuild, so watch has no effect on that service. - Copying everything before installing dependencies. If
COPY . .comes beforeRUN npm install, every source edit reruns the install, and each rebuild is slow. - A mismatch between
WORKDIRandtarget. If the Dockerfile uses/appand a rule targets/usr/src/app, files land where the application never reads them. - Missing
.dockerignore. Without one,COPY . .also copies a hostnode_modulesinto the image and may overwrite the installed one.
Check your understanding
Exercise
A teammate's Dockerfile has COPY . . followed by RUN npm install, and the service sets WORKDIR /app. Name two problems this causes for a watch setup, and say how to fix the Dockerfile.
Show answer
First, every source change invalidates the COPY . . layer, and so the npm install layer after it. Each rebuild then reinstalls all dependencies and is slow. Second, the installed dependencies can be overwritten by a host node_modules that was copied in, unless .dockerignore excludes it. To fix it, copy package*.json first, run npm install, then run COPY . ., and add node_modules to .dockerignore.
Exercise
Your service defines only image: my-app:latest and a develop.watch section. Why will rebuild-style updates not work, and what do you change?
Show answer
With only image:, Compose has no build instructions to run, so there is nothing to rebuild when a file changes. Replace image: with a build: section (for example build: .), and make sure the Dockerfile's WORKDIR matches the targets in the watch rules.