Dockerfile Image
A Dockerfile is a “script for building an image”: one instruction per line, starting from a base image and step by step copying files, installing dependencies, and setting up the environment to produce a reproducible image. This page covers how it relates to the build context and builds one from scratch. It assumes you can already use the basic docker commands (see CLI commands).
Build context and tag
Indocker build -t myapp:1.0 .:
-t myapp:1.0: the image tag (name:version, omitting the version defaults tolatest).- The trailing
.: the build context. The builder only sees files inside the context, not paths outside it. -f: specifies the Dockerfile location (defaults toDockerfilein the context root).
.dockerignore
Same syntax as .gitignore, sits in the context root, and keeps things out of the builder, shrinking the context and avoiding needless cache invalidation:
** and ! (negate to keep). A bigger context ships slower, so .dockerignore is a file worth preparing by default.
Building a Dockerfile from scratch
Pick a base image (FROM)
Start from a suitable base, pinning a version tag rather than
latest (see Choosing images):Set the working directory (WORKDIR)
Following COPY / RUN are relative to this; the directory is auto-created if missing:
Install dependencies, copy code (COPY + RUN)
Copy the manifest first, install, then copy the source (order matters, see layer cache):
Next
- Dockerfile instructions one by one: the semantics and differences of each instruction.
- A Python app Dockerfile: a real example and multi-stage.
- Layer cache and best practices: why instruction order matters.