Images, containers and registries
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)ShopLite works on your laptop. To hand the exact same app to a teammate or a server, you need one package with the app and everything it needs, and a place to store and share it.
Images are read-only layers; containers add a writable layer
An image is a read-only template of an app and all the files it needs, made of stacked layers. An image never changes once it is built.
An image is a stack of read-only layers: base OS files, a JRE, then your jar. A container adds one thin writable layer on top. The model below runs two containers from one image.
Example 1 · A model of layers and the writable layer
import java.util.HashMap;import java.util.List;import java.util.Map;
class Container { private final String name; private final List<Map<String, String>> imageLayers; private final Map<String, String> writable = new HashMap<>();
Container(String name, List<Map<String, String>> imageLayers) { this.name = name; this.imageLayers = imageLayers; }
String read(String path) { if (writable.containsKey(path)) { return writable.get(path); } for (int i = imageLayers.size() - 1; i >= 0; i--) { if (imageLayers.get(i).containsKey(path)) { return imageLayers.get(i).get(path); } } return "(no such file)"; }
void write(String path, String content) { writable.put(path, content); }
String name() { return name; }}
public class LayersDemo { public static void main(String[] args) { List<Map<String, String>> image = List.of( Map.of("/etc/os-release", "Ubuntu 24.04"), // layer 1: base OS files Map.of("/opt/java/bin/java", "JDK 25 runtime"), // layer 2: the JRE Map.of("/app/app.jar", "ShopLite 1.2.0")); // layer 3: your jar
Container a = new Container("api-1", image); Container b = new Container("api-2", image);
a.write("/tmp/orders.log", "order 101 paid"); a.write("/app/app.jar", "patched by hand");
for (Container c : List.of(a, b)) { System.out.println(c.name() + ": app.jar = " + c.read("/app/app.jar") + ", orders.log = " + c.read("/tmp/orders.log")); } System.out.println("Image layer 3 still has: " + image.get(2).get("/app/app.jar")); }}api-1: app.jar = patched by hand, orders.log = order 101 paid api-2: app.jar = ShopLite 1.2.0, orders.log = (no such file) Image layer 3 still has: ShopLite 1.2.0
What this code does
- The image is a list of three read-only maps (base OS, JRE, jar). Both containers share that same list; each has its own
writablemap. readchecks the writable layer first, then the image layers from the top down.writeonly ever touches the writable layer, which is copy-on-write.api-1"patched" the jar, butapi-2and the image still see ShopLite 1.2.0. In real Docker that hand patch disappears whenapi-1is deleted.
Each layer is a set of files added on top of the one below. When a container reads a file, Docker looks from the top layer down; when it writes, the change goes only into the container's own writable layer. This is called copy-on-write:
- Many containers can share one image on disk; each pays only for its own writable layer.
- Deleting a container deletes its writable layer, so files written there are lost.
- To change what is in the image, you build a new image; you never edit the old one.
Image and container: like a class and an object
A container is a running or stopped instance of an image, with its own thin writable layer on top. One image can start many containers.
For a Java developer, an image is like a class and a container is like an object: built once, instantiated many times. The program below starts two containers from one image.
Example 2 · One image, many containers
import java.util.ArrayList;import java.util.List;
record Image(String repository, String tag) { Container run(String containerName) { return new Container(containerName, this); }}
class Container { private final String name; private final Image image; private String state = "running";
Container(String name, Image image) { this.name = name; this.image = image; }
void stop() { state = "exited"; }
@Override public String toString() { return name + " from " + image.repository() + ":" + image.tag() + " (" + state + ")"; }}
public class ImageClassDemo { public static void main(String[] args) { Image hello = new Image("shoplite-hello", "0.1"); // one image ...
List<Container> containers = new ArrayList<>(); containers.add(hello.run("hello-1")); // ... many containers containers.add(hello.run("hello-2")); containers.add(hello.run("hello-3")); containers.get(1).stop();
containers.forEach(System.out::println); System.out.println("Images: 1, containers: " + containers.size()); }}hello-1 from shoplite-hello:0.1 (running) hello-2 from shoplite-hello:0.1 (exited) hello-3 from shoplite-hello:0.1 (running) Images: 1, containers: 3
What this code does
Imageis a record (the class side) andrun(name)creates a newContainerfrom it, just likedocker runstarts a new container from an image.- Stopping
hello-2changes only that container's state. The other two keep running and the image is untouched. shoplite-hello:0.1is the small health-server image you build in the Dockerising Spring Boot module.
Each object has its own field values; each container has its own writable layer, name and state (running or exited). Changing one object does not change the class, and stopping one container does not change the image.
Image names decoded
A tag is the version label after the colon in an image name, such as 25-jre or 8.4. If you leave it out, Docker uses latest.
A fully qualified image name has four parts: registry/namespace/repository:tag. Docker fills in whatever you leave out.
docker.io / library / eclipse-temurin : 25-jre | | | |registry namespace repository tagThe registry is the server that stores the image, the namespace is the owner (a user or organisation), the repository is the image's name and the tag is its version label.
- No registry part: Docker uses
docker.io(Docker Hub). - No namespace on Docker Hub: Docker uses
library, where the official images live. - No tag: Docker uses
latest. Somysqlmeansdocker.io/library/mysql:latest. - A first part with a dot, a colon or the word
localhostis a registry:ghcr.io/...,localhost:5000/....
Example 3 · Turning short names into full names
import java.util.List;
public class ImageNameDemo { static String fullName(String name) { String registry = "docker.io"; String rest = name; int slash = name.indexOf('/'); if (slash > 0) { String first = name.substring(0, slash); if (first.contains(".") || first.contains(":") || first.equals("localhost")) { registry = first; rest = name.substring(slash + 1); } } if (registry.equals("docker.io") && !rest.contains("/")) { rest = "library/" + rest; // official images live under library/ } String lastPart = rest.substring(rest.lastIndexOf('/') + 1); if (!lastPart.contains(":") && !lastPart.contains("@")) { rest = rest + ":latest"; // no tag given } return registry + "/" + rest; }
public static void main(String[] args) { List<String> names = List.of( "eclipse-temurin:25-jre", "mysql", "bitnami/redis:7.4", "ghcr.io/asha-rao/shoplite-api:1.2.0", "localhost:5000/shoplite-hello:0.1"); for (String n : names) { System.out.printf("%-35s -> %s%n", n, fullName(n)); } }}eclipse-temurin:25-jre -> docker.io/library/eclipse-temurin:25-jre mysql -> docker.io/library/mysql:latest bitnami/redis:7.4 -> docker.io/bitnami/redis:7.4 ghcr.io/asha-rao/shoplite-api:1.2.0 -> ghcr.io/asha-rao/shoplite-api:1.2.0 localhost:5000/shoplite-hello:0.1 -> localhost:5000/shoplite-hello:0.1
What this code does
fullNameapplies Docker's three defaults: registrydocker.io, namespacelibraryfor one-word Docker Hub names, and taglatest.- The first part counts as a registry only when it looks like a host: a dot (
ghcr.io), a port (localhost:5000) orlocalhost.bitnamiis a Docker Hub namespace. - Notice that plain
mysqlsilently becamemysql:latest, which is why short names are risky in real files.
Predict the output
Predict the full name
Decode each name the way Docker does.
Round 1 of 2
Which full name does Docker use for this command?
Registries, official images and pinned tags
A registry is a server that stores images so other computers can download (pull) and upload (push) them. Docker Hub (docker.io) is the default public one.
A registry stores images so other computers can pull and push them. Always pin your tags: mysql:8.4, not mysql:latest. The check below flags unpinned images.
Example 4 · Spotting unpinned images
import java.util.List;
public class PinCheckDemo { static String check(String name) { if (name.contains("@sha256:")) { return "pinned by digest (never changes)"; } String lastPart = name.substring(name.lastIndexOf('/') + 1); int colon = lastPart.indexOf(':'); if (colon < 0) { return "not pinned: no tag means latest"; } String tag = lastPart.substring(colon + 1); if (tag.equals("latest")) { return "not pinned: latest moves to new versions"; } return "pinned tag " + tag; }
public static void main(String[] args) { List<String> images = List.of( "eclipse-temurin:25-jre", "mysql", "redis:latest", "mysql:8.4", "eclipse-temurin@sha256:4f1d2a9c"); for (String image : images) { System.out.println(image + " -> " + check(image)); } }}eclipse-temurin:25-jre -> pinned tag 25-jre mysql -> not pinned: no tag means latest redis:latest -> not pinned: latest moves to new versions mysql:8.4 -> pinned tag 8.4 eclipse-temurin@sha256:4f1d2a9c -> pinned by digest (never changes)
What this code does
checklooks at the last part of the name only, so a registry port such aslocalhost:5000is not mistaken for a tag.- No tag and
latestboth mean "whatever is newest when you pull", so they are flagged. - The digest here is shortened to fit; a real
sha256digest has 64 hex characters.
Docker Hub (docker.io) is the default public registry. GitHub Container Registry (ghcr.io) stores images next to your code, and every cloud has its own, such as AWS ECR, Google Artifact Registry and Azure Container Registry. Official images (eclipse-temurin, mysql, alpine) are maintained with Docker and the software's own team; use them as your base whenever you can.
The latest tag is just a label that moves to each new release, so the same file can pull MySQL 8.4 today and 9.x next month. A pinned tag such as 8.4 still gets small patch updates; a digest (next section) never changes at all.
Find the bug
Find the unpinned image
Click the line that can change under you.
Which image line is not pinned?
Round 1 of 2docker pull, docker images and digests
A digest is a sha256:... fingerprint calculated from the exact contents of an image. If one byte changes, the digest changes, so a digest always means the same bytes.
docker pull downloads an image from a registry, layer by layer, and docker images lists what is already on your computer. Your layer IDs, digests, sizes and dates will differ.
Example 5 · docker pull, twice
Runs in: a terminal with Docker installed
# First pull: downloads every layerdocker pull eclipse-temurin:25-jre# Second pull of the same tag: nothing new to downloaddocker pull eclipse-temurin:25-jre25-jre: Pulling from library/eclipse-temurin a448908e17d6: Pull complete dd96e449ced8: Pull complete be4366b90790: Pull complete abdf638c9fd0: Pull complete 1c19b1a77799: Pull complete Digest: sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787 Status: Downloaded newer image for eclipse-temurin:25-jre docker.io/library/eclipse-temurin:25-jre 25-jre: Pulling from library/eclipse-temurin Digest: sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787 Status: Image is up to date for eclipse-temurin:25-jre docker.io/library/eclipse-temurin:25-jre
What this code does
- The first pull downloads five layers (one line each), then prints the image digest and the full name
docker.io/library/eclipse-temurin:25-jre. - The second pull finds the same digest locally and downloads nothing: "Image is up to date". Layers are also shared between images, so a second image built on the same base downloads only its new layers.
- Layer IDs, digest and the number of layers will differ on your computer and change when the image is updated.
Example 6 · docker images and digests
Runs in: a terminal with Docker installed
# List the images on this computerdocker images# Show the digest of one repository's imagesdocker images --digests eclipse-temurin# Pull by digest: always exactly the same bytesdocker pull eclipse-temurin@sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787REPOSITORY TAG IMAGE ID CREATED SIZE eclipse-temurin 25-jre f2cef480ee3e 2 weeks ago 276MB mysql 8.4 1452f7c5f22d 3 weeks ago 769MB alpine 3.20 abdf638c9fd0 5 weeks ago 7.8MB REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE eclipse-temurin 25-jre sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787 f2cef480ee3e 2 weeks ago 276MB docker.io/library/eclipse-temurin@sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787: Pulling from library/eclipse-temurin Digest: sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787 Status: Image is up to date for eclipse-temurin@sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787 docker.io/library/eclipse-temurin@sha256:3bda84624059b776d908a2b70f3b5ead0be7ad43a3a61662d08184781348e787
What this code does
docker imagesshows repository, tag, a short image ID, age and size.--digestsadds thesha256digest, which you can paste into a name with@.- Pulling by digest gives exactly those bytes even if someone later moves the
25-jretag to a new build. - This is the Docker 27 table layout; newer Docker versions may show a different layout, and your IDs, sizes and dates will differ.
A tag can be moved to a new image; a digest never moves. You will run these commands yourself once Docker is installed in the next lesson.
Next step
Check your understanding
11 quick questions