Card 01/ 06
Dockerfiles: The Recipe for an Image
A text file that turns your project into an image that runs the same way on every machine.
A Dockerfile is a text file of instructions that tells docker build how to produce an image: which image to start from, which files to copy in, which commands to run, and which program to start when a container, a running copy of the image, starts. It is how your own code becomes an image that anyone can run.
Running someone else's image needs only docker run. Running your own application in a container needs an image of it first, and the only thing you write to get one is this file, usually named Dockerfile at the top of your project.
The instructions are read top to bottom. Each one takes the filesystem left by the instruction above it, changes something, and hands the result down. What comes out of the bottom is the image. A Dockerfile for a Node.js web application, for instance, starts from an image that already has Node.js installed, copies in the project's files, installs its packages, and names the command that starts the server.
It works like a recipe. The base image is the ingredients you start with, each instruction is one step of the method, and the image is the finished dish. Like a recipe, the file describes what to do rather than doing it: docker build follows it, and the builder never improvises. The comparison stops at the ingredients. A recipe gives the same dish only if the ingredients are the same, and a Dockerfile gives the same image only if the base image and the packages it downloads have not changed.
So a Dockerfile replaces a setup document: instead of steps someone follows by hand, it is a file a machine follows exactly, on a computer that has never seen your project.
← → move through sections and cards