Skip to content

App1 · REST Receiver

App1 - Tiny REST Receiver

A minimal HTTP service with zero dependencies - just Node.js and the standard library. This is the “simplest useful container” for the course: a few lines of code in a few lines of Dockerfile.

What it does

Endpoint Method Response
/health GET {"status":"healthy","service":"app1"}
/api/receive POST Echoes the request body back, plus a running request_count
/ GET Info page

app1/server.js

server.js
const http = require("http");

let requestCount = 0;

const server = http.createServer((req, res) => {
  if (req.url === "/health") {
    res.writeHead(200, { "Content-Type": "application/json" });
    return res.end(JSON.stringify({ status: "healthy", service: "app1" }));
  }

  if (req.url === "/api/receive" && req.method === "POST") {
    let body = "";
    req.on("data", (chunk) => (body += chunk));
    return req.on("end", () => {
      let payload;
      try {
        payload = body ? JSON.parse(body) : null;
      } catch {
        payload = body; // not JSON - echo the raw text
      }
      requestCount += 1;
      res.writeHead(200, { "Content-Type": "application/json" });
      res.end(
        JSON.stringify({
          status: "ok",
          service: "app1",
          received: payload,
          request_count: requestCount,
        })
      );
    });
  }

  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("App1 - tiny REST receiver (Docker Workshop)\n");
});

server.listen(3000, () => console.log("App1 listening on :3000"));

app1/Dockerfile

Dockerfile
# The simplest possible Dockerfile for a Node.js service.
# Compare this with buggy's multi-stage Dockerfile!
FROM node:20-alpine

WORKDIR /app

COPY server.js .

# Run as the non-root "node" user that ships with the image
USER node

EXPOSE 3000

CMD ["node", "server.js"]

Run it standalone

cd src/app1
docker build -t docker-workshop-app1:latest .
docker run -d --name app1 -p 5001:3000 docker-workshop-app1:latest

curl http://localhost:5001/health
curl -X POST http://localhost:5001/api/receive \
     -H "Content-Type: application/json" \
     -d '{"message": "hello app1"}'

What to notice

Detail Why it matters
EXPOSE 3000 vs -p 5001:3000 EXPOSE is documentation only. The container listens on 3000; the host port is 5001.
USER node Containers should not run as root. The node image ships a ready-made non-root user.
No framework, no npm install Fewer moving parts = fewer things that can break. node:20-alpine already has everything we need.
/health endpoint Used by the Compose healthcheck so nginx starts only when app1 is actually ready.

Next steps