Start learning free

LESSON 150 OF 173 · BACKEND: SHIPPING IT

A Dockerfile: The Base Image

Practice

To run the shop the same way on any server, you pack it into a Docker image: a snapshot of a small Linux system with Java, the packages and your code in it. A container is that image running. The recipe for an image is a file named Dockerfile, one instruction per line. Here you write its text as the Java array dockerfile, one string for each line of the file.

A Dockerfile starts with FROM, the image you build on, written name:tag: the tag after the colon is the version. The tag of the maven image names two versions: 3.9-eclipse-temurin-21 is Maven 3.9 with Java 21 from Eclipse Temurin. WORKDIR picks the folder where the next lines work and the app runs later; Docker creates it if it is missing. ENV sets an environment variable, the kind System.getenv reads, written NAME=value. Here is the start of a Dockerfile for another program, built on Node.js:

static String[] dockerfile = {
    "FROM node:22-slim",
    "WORKDIR /srv",
    "ENV TZ=UTC",
};

One variable matters for the shop. Unless a setting says otherwise, a Spring Boot app listens on port 8080. Spring Boot also reads its settings from the environment, and SERVER_PORT is its port setting: SERVER_PORT=8000 makes the shop listen on port 8000.

The check reads your lines the way docker build would. When they are right, it prints:

$ docker build -t shop .
[1/3] FROM maven:3.9-eclipse-temurin-21: starts from an Ubuntu Linux with Java 21 (Eclipse Temurin) and Maven 3.9 installed: the tools that build the shop and run it
[2/3] WORKDIR /app: makes the folder /app and works in it; the next lines and the app run there
[3/3] ENV SERVER_PORT=8000: sets SERVER_PORT for every program in the image, so the shop listens on port 8000 (Spring Boot's default is 8080)
The base is ready: maven:3.9-eclipse-temurin-21, working in /app. The files of the shop come next.

Your task: fill the array dockerfile with three lines: start FROM the official Maven image, tag 3.9-eclipse-temurin-21; work in the folder /app; and set SERVER_PORT to 8000.


Assignment
Follow the Coding Tutorial to write the first three lines of the shop's Dockerfile: FROM, WORKDIR and ENV.


Hint
Each string of the array is one line of the file, with a comma after it. Write the folder as a full path that starts with /, and NAME=value with no spaces around =.


STEP 1 OF 3
    Ctrl + Enter to check