Docker Fundamentals: Build and Run a Containerized App
Build a small Node.js container with Docker, run it through Compose, check its health, and confirm that its named volume keeps data across restarts.
Build a tiny Node.js service into a non-root, multi-stage image, then run it with Docker Compose. The sample reports health, writes a visit counter to a named volume, and shows how to distinguish a local image ID from a registry digest.
Docker fundamentals: build and run an app
This hands-on guide builds one small Node.js service, packages it in a non-root image, and runs it with Docker Compose. It gives you a working path through the image and container basics; the linked guides cover Compose, networking, and volumes in more depth.
The snippets use the Compose Specification without a top-level version field. The command examples target Docker Compose v5.7.1 and Docker Engine 29.9.3. Those CLI and Engine versions are current references for this guide, not a requirement of the Compose file format. Check the Compose release history and Engine release notes when matching a different installation.
1. Create the sample app
Create an empty directory with these files:
docker-fundamentals-lab/
├── build.js
├── compose.yaml
├── Dockerfile
├── package.json
├── package-lock.json
└── server.js
package.json declares a build step and has no external dependencies:
{
"name": "docker-fundamentals-lab",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "node build.js"
}
}
Use this matching package-lock.json so npm ci has a lockfile to read:
{
"name": "docker-fundamentals-lab",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "docker-fundamentals-lab",
"version": "1.0.0"
}
}
}
The build step copies the service into a runtime directory. Real front-end or compiled applications can put their build output there instead.
// build.js
const fs = require("node:fs");
fs.mkdirSync("dist", { recursive: true });
fs.copyFileSync("server.js", "dist/server.js");
The service exposes a health endpoint and a visit counter. The counter lives under /data, which Compose will mount as a named volume.
// server.js
const fs = require("node:fs");
const http = require("node:http");
const dataFile = "/data/visits.txt";
const server = http.createServer((request, response) => {
response.setHeader("content-type", "application/json");
if (request.url === "/health") {
response.writeHead(200);
response.end(JSON.stringify({ status: "ok" }));
return;
}
if (request.url === "/visits") {
let visits = 0;
try {
visits = Number.parseInt(fs.readFileSync(dataFile, "utf8"), 10) || 0;
} catch (error) {
if (error.code !== "ENOENT") throw error;
}
visits += 1;
fs.writeFileSync(dataFile, String(visits));
response.writeHead(200);
response.end(JSON.stringify({ visits }));
return;
}
response.writeHead(404);
response.end(JSON.stringify({ error: "not found" }));
});
server.listen(3000, "0.0.0.0");
2. Build a non-root image
The build stage runs the package build. The runtime stage copies only the generated service, creates a writable data directory, and runs as Node’s unprivileged node user. Both stages pin the same Node 22 Alpine image index digest; update the digest deliberately when you update the base image.
FROM node:22-alpine@sha256:5b05da3f938f1c604b24c633646d48805a6667f378cb4908438616f3cc48a0bf AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY build.js server.js ./
RUN npm run build
FROM node:22-alpine@sha256:5b05da3f938f1c604b24c633646d48805a6667f378cb4908438616f3cc48a0bf AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build --chown=node:node /app/dist ./dist
RUN mkdir -p /data && chown node:node /data
USER node
EXPOSE 3000
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 CMD ["node", "-e", "fetch('http://127.0.0.1:3000/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"]
CMD ["node", "dist/server.js"]
A tag such as node:22-alpine can move when the publisher updates it. A digest identifies the selected image index, so builds keep using that index until you change the digest. Check the official Node image before updating it and rebuild regularly to pick up security fixes.
3. Run it with Compose
Create compose.yaml. The port binds to loopback so the lab service is reachable from this machine only. The named volume keeps the counter when the container is recreated.
name: docker-fundamentals-lab
services:
app:
build: .
image: docker-fundamentals-lab:local
ports:
- "127.0.0.1:3000:3000"
volumes:
- visit_data:/data
restart: unless-stopped
volumes:
visit_data:
Compose reads service dependencies in order, but startup order alone does not mean a service is ready. This one-service sample has no dependency; see the Compose guide for health-gated startup and multi-service examples.
4. Check health and persistence
From the directory containing compose.yaml, build and start the app:
docker compose up --build -d
docker compose ps
curl --fail http://127.0.0.1:3000/health
curl --fail http://127.0.0.1:3000/visits
curl --fail http://127.0.0.1:3000/visits
The health request should return {"status":"ok"}. Each visit request increments the counter. Recreate the container without removing the named volume:
docker compose down
docker compose up --build -d
curl --fail http://127.0.0.1:3000/visits
The count continues from its previous value. docker compose down preserves named volumes; adding -v would delete the lab’s stored counter.
Check the local image identifier:
docker image inspect --format '{{.Id}}' docker-fundamentals-lab:local
This prints the local image configuration ID, which begins with sha256:. It is not the registry manifest digest. After pushing an image to a registry, inspect the digest that clients will pull with:
docker buildx imagetools inspect registry.example.com/your-user/docker-fundamentals-lab:1.0.0
Use the digest shown by that command when you need to refer to the published image immutably. Do not copy a sample digest into a deployment; it must come from the image you built and pushed.
Where to go next
- Short Docker fundamentals overview for core terms and trade-offs.
- Docker Compose guide for service dependencies, secrets, overrides, and scaling limits.
- Docker networking guide for bridge networks, host networking, and port publishing.
- Docker volumes guide for storage behavior, backup, and restore.
- Dockerfile reference and Compose file reference for the current syntax.
Category
Related Posts
Docker Fundamentals
Learn Docker containerization fundamentals: images, containers, volumes, networking, and best practices for building and deploying applications.
Container Images: Building, Optimizing, and Distributing
Learn how Docker container images work, layer caching strategies, image optimization techniques, and how to publish your own images to container registries.
Container Registry: Image Storage, Scanning & Distribution
Set up and secure container registries for storing, scanning, and distributing container images across your CI/CD pipeline and clusters.