Foundation project

Project: a production-minded application image

Lesson 10 of 20 · 22 min · Module 3: Persistence and a first project

Combine cache-aware assembly, nonroot execution, and an explicit runtime contract.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Plan the build inputs
  • Reduce unnecessary runtime privileges

The idea

Image instructions are an ordered build contract. A later instruction can change a default established earlier; the final stage determines the output image.

01Plan the build inputs

List what dependency installation needs, then what application startup needs. This keeps the installation cache independent of routine source edits.

02Reduce unnecessary runtime privileges

The runtime needs application files and production dependencies. It should not need root merely because the build used a privileged default.

03Review the complete contract

Check the final directory, user, configuration, port, and command together. A correct-looking CMD cannot compensate for missing files in a real build.

Your project brief

Build from node:22-alpine in /app. Copy package.json and package-lock.json, run npm ci --omit=dev, then copy src/ to ./src/. Set NODE_ENV=production, use USER node, EXPOSE 3000, and CMD ["node","src/server.js"].

What the checks look for

  • Uses /app and the requested base
  • Installs production dependencies after manifests
  • Copies source after installation
  • Configures a nonroot production process
Try it in the editor →

Take this with you

A small explicit contract makes an image easier to inspect and operate.

Go a little further

Add a health command and explain which file it expects in the image.

Learn Docker & Containers for free

20 short lessons with real code and instant checks.

See the whole course →