Start learning free

LESSON 150 OF 173 · BACKEND: SHIPPING IT

A Dockerfile: The Base Image in TypeScript

Practice

To run the shop the same way on any server, you pack it into a Docker image: a snapshot of a small Linux system with Node.js, the packages and your code in it. A container is that image running. The recipe for an image is a file named Dockerfile, one instruction per line. Here you write its text as the string dockerfile: an array of strings (string[]), one per line of the file, and join with \n, the line break, makes it one string.

A Dockerfile starts with FROM, the image you build on, written name:tag: the tag after the colon is the version, and -slim marks the small variant. WORKDIR picks the folder where the next lines work and the app runs later; Docker creates it if it is missing. ENV sets an environment variable, the kind process.env reads, written NAME=value. Here is the start of a Dockerfile for another program, built on Python:

const dockerfile = [
  "FROM python:3.13-slim",
  "WORKDIR /srv",
  "ENV TZ=UTC",
].join("\n");

One variable matters for Express in a container: NODE_ENV. Without it, Express runs in development mode: when a route crashes, the answer shows the error with the names of your files and the lines where it happened. That is handy for you and a gift to an attacker. With NODE_ENV=production, the visitor gets only Internal Server Error, and the details go to the log.

The check reads your lines the way docker build would. When they are right, it prints:

$ docker build -t shop .
[1/3] FROM node:22-slim: starts from a small Debian Linux with Node.js 22 installed and little else
[2/3] WORKDIR /app: makes the folder /app and works in it; the next lines and the app run there
[3/3] ENV NODE_ENV=production: sets NODE_ENV for every program in the image, so Express knows it runs for real visitors and keeps the details of an error out of its answers
The base is ready: node:22-slim, working in /app. The files of the shop come next.

Your task: write the string dockerfile with three lines: start FROM the official Node.js image, version 22, in its slim variant; work in the folder /app; and set NODE_ENV to production.


Assignment
Follow the Coding Tutorial to write the first three lines of the shop's Dockerfile: FROM, WORKDIR and ENV.


Hint
Each string in the array is one line of the file, with a comma after it. Write the folder as a full path that starts with /, and NAME=value with no spaces around =.


STEP 1 OF 3
    Ctrl + Enter to check

    Ship Dockerfile Base in TypeScript

    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free