Docker lab/version note: use supported Docker and Node.js tools and patched images. The JavaScript examples have local tests; Docker, Nginx and hosted CI steps were not tested live.
What this guide covers
During development, you can mount source files into a container and let Node.js restart when a file changes. This avoids rebuilding the image for every source edit.
Before you start
Install Docker with the Compose plugin and use a trusted local working directory. The example is a small web server that returns a message; it has no database or external dependencies. Save each file at the path given below.
Step 1: Create the application files
Create src/handler.mjs:
export function handler(req, res) {
const path = new URL(req.url, 'http://example.invalid').pathname;
res.setHeader('Content-Type', 'application/json');
if (path === '/healthz') return res.end(JSON.stringify({ ok: true }));
if (path === '/api/message') return res.end(JSON.stringify({ message: 'Hello' }));
res.statusCode = 404;
res.end(JSON.stringify({ error: 'Not found' }));
}
Create src/server.mjs:
import http from 'node:http';
import { handler } from './handler.mjs';
const server = http.createServer(handler);
server.listen(Number(process.env.PORT || 3210), '0.0.0.0');
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
setTimeout(() => process.exit(1), 8000).unref();
});
Create src/handler.test.mjs:
import test from 'node:test';
import assert from 'node:assert/strict';
import { handler } from './handler.mjs';
function request(url) {
const result = { statusCode: 200, setHeader() {}, end(body) { this.body = JSON.parse(body); } };
handler({ url }, result);
return result;
}
test('returns the message', () => {
assert.deepEqual(request('/api/message').body, { message: 'Hello' });
});
test('health check succeeds', () => {
assert.equal(request('/healthz').body.ok, true);
});
test('unknown paths return 404', () => {
assert.equal(request('/missing').statusCode, 404);
});
The handler returns a JSON message at /api/message, reports health at /healthz, and returns 404 for unknown paths. Keeping it separate lets tests call it without starting a server.
Step 2: Create the development image
Save this as Dockerfile.dev in the project root:
FROM node:24-bookworm-slim
WORKDIR /workspace
COPY --chown=node:node src ./src
USER node
EXPOSE 3210
CMD ["node", "src/server.mjs"]
Save this as compose.dev.yaml:
services:
web-app:
build:
context: .
dockerfile: Dockerfile.dev
image: example-web:dev
command: ['node', '--watch', 'src/server.mjs']
environment:
PORT: '3210'
ports: ['127.0.0.1:3210:3210']
volumes: ['./src:/workspace/src:ro']
init: true
app-test:
image: example-web:dev
command: ['node', '--test', 'src/handler.test.mjs']
volumes: ['./src:/workspace/src:ro']
profiles: [test]
The source mount makes host edits visible to the container. node --watch restarts the application when files change. The port is published on the host’s local address only.
Step 3: Build, start and test
Run from the project root:
docker compose -f compose.dev.yaml config --quiet
docker compose -f compose.dev.yaml build web-app
docker compose -f compose.dev.yaml up -d web-app
curl --fail http://127.0.0.1:3210/api/message
docker compose -f compose.dev.yaml run --rm app-test
docker compose -f compose.dev.yaml logs --tail 50 web-app
The API should return a JSON message, and the test service should pass. Change the message in src/handler.mjs, save it, and request /api/message again to see the new value.
Step 4: Stop the development environment
Run docker compose -f compose.dev.yaml down. Rebuild the image if its base image or copied non-mounted files change. Production should use copied, tested source rather than a live source mount.
Check the result
Confirm a source edit appears without rebuilding and an unknown URL returns HTTP 404. A running container alone is not enough to show that file watching works.
Conversation
0 commentsNo comments yet. Start the conversation below.