Build images
Create the Notes API
Create the small Node.js HTTP service that will become the running project for the rest of the Docker course.
8 minute lesson
The course project is a tiny Notes API. Keeping the application small lets us see which behavior comes from Node.js and which comes from Docker.
The server reads its port from the environment and binds to 0.0.0.0. Binding only to localhost inside a container would make the process unreachable through Docker’s virtual network.
import { createServer } from 'node:http'
const notes = [{ id: 1, text: 'Learn containers' }]
const port = Number(process.env.PORT ?? 3000)
createServer((request, response) => {
response.setHeader('content-type', 'application/json')
response.end(JSON.stringify({ notes }))
}).listen(port, '0.0.0.0')
Keep the first version observable and easy to stop. Reject an invalid port during startup, log fatal startup errors to standard error, and let the process respond to termination instead of hiding work in detached child processes. Docker can only judge the lifecycle of the main process it started.
The host binding is part of the container contract. 0.0.0.0 accepts traffic arriving on the container’s network interface; 127.0.0.1 would accept traffic only from inside that container. Test the host process first, then use the same curl request after containerizing it. The changed boundary is then the only new variable.
Create server.js, add "type": "module" to package.json, and run the server directly with Node before involving Docker.
Lesson completed