Your first Dockerfile

Docker · lesson 3 of 31 · 4 min read

Write a Dockerfile that packages an executable Java jar and starts it the right way.

Open this lesson in the learning hub

Key points

  • FROM picks the base, WORKDIR sets the directory, COPY brings files in, ENTRYPOINT says what runs.
  • Use the exec form ENTRYPOINT ["java","-jar","app.jar"]. The shell form wraps you in /bin/sh, which swallows SIGTERM.
  • ENTRYPOINT is the command, CMD is its default arguments. Users override CMD easily and ENTRYPOINT rarely.
  • EXPOSE is documentation. It publishes nothing, -p does that.
  • Run as a non-root USER. Most bases default to root, and root in a container is root on your mounted volumes.

Example

# Dockerfile
FROM eclipse-temurin:21-jre

WORKDIR /app
COPY target/app.jar app.jar

# unprivileged user, fixed uid so volume permissions stay predictable
RUN useradd --system --uid 10001 appuser
USER 10001

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

# build and tag it:
#   docker build -t myapp:1.0 .

Exec-form ENTRYPOINT, non-root USER, one jar. That is a real Dockerfile.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Docker course, and every lesson in it is listed on the Docker contents page.