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


