Docker for Kubernetes · Reference
Dockerfile cheat sheet
This sheet grows with each lesson. The full semantics are in the Dockerfile reference.
The minimal Java service
FROM eclipse-temurin:21-jdk # base image; -jdk has javac, -jre only runs Java
WORKDIR /app # created if missing; later paths are relative to it
COPY App.java . # from the build context into /app
RUN javac App.java # runs at build time, saved as a layer
EXPOSE 8080 # documentation only
CMD ["java", "App"] # default command at run time, exec form
The multi-stage Java service (Maven)
FROM maven:3.9-eclipse-temurin-21 AS build # stage 1: has Maven and the JDK
WORKDIR /app
COPY pom.xml . # alone, so code edits don't touch it
RUN mvn -B dependency:go-offline # expensive, cached until pom.xml changes
COPY src ./src # changes often, so it comes late
RUN mvn -B package # writes /app/target/app.jar
FROM eclipse-temurin:21-jre # stage 2, the image you ship: no compiler
WORKDIR /app # each stage starts fresh; set it again
COPY --from=build /app/target/app.jar . # take only the jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
Next to it, a .dockerignore containing target/ and .git. In testing, the single-stage Maven image was 590 MB and this one 341 MB, nearly all of it the shared JRE base.
The build cache
- A step is reused if its inputs are unchanged. For
COPY, the input is the contents of the copied files. - A changed step reruns, and so does every step after it in that stage.
- Order: base → dependency manifest (
pom.xml) → download → source → compile. Expensive and rarely changed steps go first. - Each stage is cached separately. A new runtime base doesn't rerun Maven.
- See it:
docker build --progress=plain …printsCACHEDnext to each reused step.
Instructions
| Instruction | Happens at | Remember |
|---|---|---|
FROM | Build | Must be first. Pin a tag, such as 21-jdk. Each FROM starts a new stage; AS name names it. The image is the last stage. |
WORKDIR | Build | Use it instead of RUN cd …. Also the container's working directory. |
COPY | Build | Sources are relative to the build context, never outside it. COPY --from=build /path . copies from an earlier stage instead. |
RUN | Build | Must finish. Never start the service here. |
EXPOSE | Recorded | Doesn't publish. Use docker run -p. |
CMD | Run | Only the last one counts. Replaces the base image's CMD. Overridden by a command after the image name in docker run, or by args in Kubernetes. |
Exec form vs shell form
| Exec form | Shell form | |
|---|---|---|
| Written | CMD ["java", "App"] | CMD java App |
| Actually runs | java App | /bin/sh -c "java App" |
| PID 1 | Java | The shell |
docker stop | Java gets SIGTERM, exits fast: Exited (143) | Signal ignored, killed after 10 s: Exited (137) |
| Use for | CMD, always | RUN, where shell features like && help |
Exit codes above 128 mean "killed by a signal": 128 + 15 (SIGTERM) = 143; 128 + 9 (SIGKILL) = 137.
Common mistakes
RUN java App: the build hangs, because a server never finishes.- COPY before WORKDIR: the file lands in the base image's working directory, not
/app. - A
-jrebase withRUN javac:javacdoesn't exist there. COPY . .before the Maven build: every edit, even to a README, reruns the build and its downloads.- Copying
srcbeforepom.xml: the dependency download reruns on every code change. - A JDK or Maven image as the final stage: you ship a compiler and build tools you never run.
- Forgetting
WORKDIRin the second stage: stages don't inherit settings from each other. - Single quotes in exec form,
CMD ['java', 'App']: that isn't valid JSON, so Docker silently treats it as shell form and runs/bin/sh -c "['java', 'App']".