FROM & WORKDIR

Choose a base and a working directory

Lesson 4 of 20 · 12 min · Module 2: Build an image contract

A Dockerfile describes how an image should be assembled.

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…

  • Choose an explicit base
  • Make relative paths predictable

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.

FROM node:22-alpine
WORKDIR /service
CMD ["node", "index.js"]

01Choose an explicit base

FROM starts a stage. A version tag narrows what you intend to use, although tags can still move; immutable digests provide stronger identity.

02Make relative paths predictable

WORKDIR sets the directory for later RUN, COPY destinations, and the startup command. It also creates a missing directory during a real image build.

03Declare startup in exec form

A JSON array records the executable and arguments separately. The practice lab parses the instructions and models their configuration; it does not run a Docker build.

Your turn

Write a Dockerfile based on node:22-alpine. Set the working directory to /app and use exec-form CMD to start node server.js.

What the checks look for

  • Uses the specified base
  • Works in /app
  • Starts Node in exec form
Try it in the editor →

Take this with you

State the base, working directory, and startup contract explicitly.

Go a little further

Add a second relative WORKDIR and predict the resulting absolute directory.

Learn Docker & Containers for free

20 short lessons with real code and instant checks.

See the whole course →