liveBook · Manning

Dockerfile instruction

As explained in Learn Docker in a Month of Lunches, Second Edition

A Dockerfile instruction is a declarative directive that specifies a build action or image metadata.

A Dockerfile instruction tells Docker what to do while building an image or how to configure that image. A few instructions can package an application for a container; commands run during the build can also create files that become part of the image. section 4.0

Common instructions have different jobs:

  • FROM selects a base image and can start a named build stage. WORKDIR sets the working directory. section 4.2
  • COPY brings files into the image. RUN executes a build command, such as fetching dependencies or packaging an application. section 4.2
  • CMD or ENTRYPOINT specifies the application command; ENV sets environment variables. EXPOSE declares a port in the Java example. section 3.5 section 4.2

Instruction order affects rebuild time. Docker checks an instruction and its inputs against cached layers. If a changed file causes a COPY step to miss the cache, that step and every later instruction run again. Put setup that rarely changes near the start and frequently edited application files near the end. section 3.5 section 17.4

Check a rebuild

Add a blank line to app.js, then run docker image build -t web-ping:v2 . Earlier unchanged steps can use cached layers; the COPY step for app.js produces a new layer, and later steps run again. This makes a small edit a useful test of instruction order. section 3.5

Watch what each instruction leaves behind. Copying an unnecessary folder and deleting it with a later RUN hides the files but does not remove their bytes from the earlier layer. Copy only what the application needs; use docker history to inspect layer sizes. Also prefer cross-platform Dockerfile instructions when an equivalent RUN command depends on the operating system. section 17.1 section 12.6

Listing 4.2 Dockerfile for building a Java app with Maven
FROM diamol/maven:2e AS builder

WORKDIR /usr/src/iotd
COPY pom.xml .
RUN mvn -B dependency:go-offline

COPY . .
RUN mvn package

# app
FROM diamol/openjdk:2e

WORKDIR /app
COPY --from=builder /usr/src/iotd/target/iotd-service-0.1.0.jar .

EXPOSE 80
ENTRYPOINT ["java", "-jar", "/app/iotd-service-0.1.0.jar"]

Follow a complete multi-stage Dockerfile: the builder copies the Maven definition before the source code so dependency fetching can stay cached, then the final stage copies in the JAR. section 4.2

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains instruction order through cache reuse, layer contents, and worked builds; does not provide a full instruction reference.
Examples
Uses an edited JavaScript file, a Maven-built Java application, and copied files that remain in an image after deletion.
What it assumes
Starts from basic Docker image builds and introduces build-time commands; explains the Java tooling needed for its Maven example.
What you can do after
Build it from scratch: write and reorder Dockerfile instructions to package an application while reducing rebuild work and unwanted image contents.
How it connects
Connects Dockerfile instructions to image layers, build cache, multi-stage builds, and cross-platform images.

Also explained in

Compare how 3 books explain Dockerfile instruction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.