In-Class Exercise - Parameterized Node.js Time Server¶

Build a lightweight Node.js web server image that prints the current time and listens on a build-time configurable port.
Task Checklist¶
- Use the minimal Node.js HTTP server (
server.js) in the task folder. - Use a Docker build argument to set the port value (
LISTEN_PORT) and use it in the DockerFile - Verify that the container print the message when it called
Requirements && Tips
docker buildaccepts--build-arg LISTEN_PORT=<port>and succeeds without manual edits- Running the image with
docker run --rm -p <port>:<port>serves the current time on the chosen port - Changing
LISTEN_PORTduring build changes both the exposed port in the image metadata and the runtime listener - The container process logs which port it is using when it starts
- Use
ARGin the Dockerfile to capture the build-time value andENVto pass it to the Node.js process - Always bind to
0.0.0.0so Docker can forward traffic into the container - A
curlrequest ordocker run --rm image curl http://localhost:<port>is a quick way to verify the response
Additional Task
- Once you have completed the task, change the port and test it again to ensure the server responds on the new port.
Solution
1. Project Files¶
server.js
const http = require("http");
const requestedPort = parseInt(process.env.LISTEN_PORT || "8080", 10);
const port = Number.isNaN(requestedPort) ? 8080 : requestedPort;
const server = http.createServer((req, res) => {
const message = `Current time: ${new Date().toISOString()}\n`;
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(message);
});
server.listen(port, "0.0.0.0", () => {
console.log(`Listening on port ${port}`);
});
Dockerfile
# syntax=docker/dockerfile:1
FROM node:20-alpine
ARG LISTEN_PORT=8080
WORKDIR /usr/src/app
COPY server.js ./
ENV LISTEN_PORT=${LISTEN_PORT}
EXPOSE ${LISTEN_PORT}
CMD ["node", "server.js"]
2. Build the Image¶
3. Run and Test¶
In another terminal, verify the output:
Expected response: