A Professional First Dockerfile
A Dockerfile is a list of instructions from which Docker builds an image. In this lesson we meet the core instructions — FROM, WORKDIR, COPY, RUN, EXPOSE, and CMD — build an image with docker build, and distinguish what runs at build time from what defines how it runs.
A Dockerfile is a written recipe a kitchen follows step by step: first pick a base kitchen, then go to the work station, bring ingredients, do the prep, and finally write down what to serve when an order comes in.
- FROM
- The first instruction in a Dockerfile. It sets the base image to build on — for example node:20-alpine — and every other instruction stacks on top of it.
- WORKDIR
- Sets the working directory inside the image. From that point COPY, RUN, and CMD operate relative to it, and it is created automatically if it does not exist.
- CMD
- Defines the default command that runs when a container is started from the image. It does not run at build time, only when the container starts.