Images and building

What an image is made of, Containerfile instructions, build cache, multi-stage and non-root images, pushing to a registry, and saving and cleaning up images.
PodmanContainerfileRegistriesBuildahCore hour

Hour overview

In this hour you build the sample service from podman-app, the same way a pipeline would. You learn what each Containerfile instruction adds, how the build cache decides what is rebuilt, how to keep images small and non-root, and how to push an image to a registry and pull it back. The lab uses a local registry container, so it needs no cloud account.

Time allocation. Image internals 8, Containerfile 12, cache 10, multi-stage and non-root 12, registries 10, save and cleanup 8.

Objectives. By the end of the hour you can:

  • read an image's layers, default user and command with podman history and podman image inspect
  • explain what each Containerfile instruction contributes
  • order instructions to make the build cache work for you
  • build an image with a version tag, push it and pull it back by name
  • save an image to a tar archive, load it, and free disk space safely

Lab. Build, run, push, pull and clean up the sample service. Hour 2 uses the podman-app folder from the course site.

Modules

Image internals

Hour 2 · 00–08 · 8 min

An image is a stack of layers. Each layer is the set of files changed by one instruction. Layers are read-only and can be shared by many images. When a container starts, Podman adds one thin writable layer on top. Writes go to that layer, and the image below stays unchanged. This is called copy-on-write, and the storage driver (overlay on Linux) implements it.

writable layer      <- container: writes land here, removed with the container
--------------------
layer 3: COPY app.py       <- read-only, shared
layer 2: RUN mkdir /data   <- read-only, shared
layer 1: base image (python:3.12-alpine)

An image also has metadata: the default command, the user, environment variables and exposed ports. The content is identified by a digest, so two identical layers are stored once.

Look at a base image's history and its metadata:

podman history docker.io/library/python:3.12-alpine
podman image inspect docker.io/library/python:3.12-alpine

Expected: history prints one row per layer, newest first, with the instruction that created it and its size. inspect prints JSON. Look for the RootFS section (the layers) and the Config section (command, environment, user).

Why it matters. Layers explain three things you will meet later: why a rebuild is fast when only the top layers change, why a deleted file in one layer still takes space in an earlier one, and why a secret copied into any layer leaks even if a later layer deletes it.

Deleting a file in a later layer does not remove it from the earlier layer that holds it. Keep secrets out of the build context, not just out of the final layer.

Production note. Prefer small base images and check their history before you trust them. A base image you did not build is part of your supply chain.

Containerfile instructions

Hour 2 · 08–20 · 12 min

A Containerfile is a list of instructions. Podman runs them in order, and each instruction that changes files creates a layer. Podman reads Containerfile by default, and it also accepts Dockerfile with the same syntax.

Open podman-app/Containerfile and read it line by line:

InstructionWhat it does in the sample
FROMstarts from python:3.12-alpine, a small Python image
WORKDIR /appsets the working directory and creates it
COPY app.py .copies the one source file into /app
RUN mkdir -p /data && chown 10001:10001 /datacreates the data directory and gives it to the service user
ENVsets default environment variables
EXPOSE 8000documents the port. It does not publish it
USER 10001:10001the process runs as this numeric user
CMDthe default command when the container starts

Build the image and give it a version tag:

cd podman-app
podman build -t localhost/podman-app:dev .

Expected: Podman prints one STEP line for each instruction, and a final line with the image ID. The first build pulls the base image, so it is slower.

Run it in the background, check it, and remove it:

podman run -d --name app -p 127.0.0.1:8000:8000 localhost/podman-app:dev
curl -s http://127.0.0.1:8000/health
curl -s http://127.0.0.1:8000/
curl -s http://127.0.0.1:8000/whoami
podman rm -f app

Expected:

  • /health prints {"status": "ok"}.
  • The first / prints {"message": "hello from podman", "visits": 1}. A second call prints visits: 2, because the counter is stored in /data inside the container.
  • /whoami prints "uid": 10001, which proves the process is not root.

Check the image metadata that the build set:

podman image inspect --format '{{.Config.User}}' localhost/podman-app:dev

Expected: 10001:10001.

EXPOSE is documentation for people and tools. The -p option is what publishes the port. The sample's chown line matters: a new named volume copies its initial owner from the image directory, which is what lets the non-root user write to it in hour 4.

Common errors.

  • no such file or directory on COPY: the build context is the wrong folder. Run podman build from the folder that holds app.py, and check the . at the end.
  • Error: unable to find localhost/podman-app:dev: you ran the image before building it, or you used a different tag.

Build cache and .containerignore

Hour 2 · 20–30 · 10 min

Podman keeps each layer in the build cache. When you build again, a step is reused if its instruction and its inputs have not changed. Once one step changes, every later step is rebuilt. So the order of instructions decides how fast your rebuilds are.

Build the same image again without changing anything:

podman build -t localhost/podman-app:dev .

Expected: each step prints --> Using cache and the build finishes in a second or two.

Change app.py (for example, change the greeting text), then build again. Expected: the COPY app.py . step and the steps after it rebuild. The steps before it, such as the base image, still come from the cache. Put steps that change rarely, such as installing dependencies, above the steps that change often.

Force a full rebuild when you want fresh package updates:

podman build --no-cache -t localhost/podman-app:dev .

The build context. The . at the end is the context: the folder Podman can read files from. A COPY . . instruction copies all of it into the image. .containerignore (and .dockerignore, which Podman also reads) lists files to leave out. The sample copies only app.py, so the ignore file has no effect on this image. Try the change yourself: replace COPY app.py . with COPY . ., create a file called .env with a fake secret, build, and check that .env is in the image with podman run --rm localhost/podman-app:dev ls -a /app. Then add .env to .containerignore and build again.

A .env file with real credentials copied into the image is a leak. Keep secrets out of the context, and pass them at run time (hour 7).

Common errors.

  • The cache is not used for a step you expected to be cached: a file the step copies changed, or an earlier step changed. Compare the step order.
  • The build is slow every time: the expensive step sits below a step that always changes. Move the rarely changing step up.

Production note. Order steps from least to most likely to change. Build in CI with a fresh cache when you need a reproducible result.

Multi-stage builds and non-root users

Hour 2 · 30–42 · 12 min

Compilers, package caches and test tools are needed to build an application, but not to run it. A multi-stage build has several FROM stages. You build in one stage, then copy only the result into a smaller final stage. The final image never contains the build tools.

This pattern is the one to use for a compiled program or for an app with dependencies that need a compiler. Here is the shape, for an app with a requirements.txt file. The sample app has no dependencies, so this is an exercise to read and adapt, not a step to run:

FROM docker.io/library/python:3.12 AS build
WORKDIR /src
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt

FROM docker.io/library/python:3.12-slim
COPY --from=build /wheels /wheels
RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels

The first stage has the full toolchain. The second stage starts from a slim base and installs the prebuilt wheels. Only the second stage becomes the image.

Non-root. The sample already runs as uid 10001 (USER 10001:10001). Check it with /whoami in the same way as hour 2. A non-root process cannot change system files, and a compromised process has fewer rights to use.

Compare sizes. Create a file named Containerfile.fat in podman-app with these four lines, which use a larger base image:

FROM docker.io/library/python:3.12
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]

Then build it and compare the sizes:

podman build -f Containerfile.fat -t localhost/podman-app:fat .
podman images localhost/podman-app

Expected: the fat image is noticeably larger than dev, because it includes the full Debian-based Python image. Remove the test image with podman rmi localhost/podman-app:fat.

Choose the base image for the job. A slim or Alpine base is good for a finished program. A full image is easier when the build needs tools at run time, but it keeps more in the image to patch.

Common errors.

  • Permission denied when the app writes to a path: the path is owned by root. Create it in the image with the service user as owner, as the sample does for /data.
  • pip: command not found in a slim stage: install from the build stage only, and copy the result.

Registries: login, tag and push

Hour 2 · 42–52 · 10 min

A registry stores images and serves them by name. Your image is only on this machine until you push it. The lab uses a local registry container on 127.0.0.1:5000, so you can practice the whole flow without an account:

podman run -d --name registry -p 127.0.0.1:5000:5000 docker.io/library/registry:2
podman tag localhost/podman-app:dev localhost:5000/podman-app:1.0.0
podman push --tls-verify=false localhost:5000/podman-app:1.0.0
curl -s http://127.0.0.1:5000/v2/podman-app/tags/list

Expected: push prints layer upload progress and finishes. The curl call prints JSON with the name podman-app and the tag 1.0.0. --tls-verify=false is only for this local lab, where the registry does not use TLS. Never use it against a real registry.

Pull it back after you remove the local copy, which proves the registry has it:

podman rmi localhost:5000/podman-app:1.0.0
podman pull --tls-verify=false localhost:5000/podman-app:1.0.0
podman images localhost:5000/podman-app

Logging in. For a private registry, log in first. Pass the token on standard input so it stays out of your shell history:

printf '%s' "$REGISTRY_TOKEN" | podman login registry.example.com --username ci-user --password-stdin
podman login --get-login registry.example.com

Expected: login prints Login Succeeded!. --get-login prints the user name the registry knows you by. Use a token with the smallest permission that works.

Tags can move. In production, push a version tag such as 1.0.0, deploy by digest, and never overwrite a tag that is already in use.

Common errors.

  • connection refused on port 5000: the registry container is not running. Check podman ps, then start it again.
  • unauthorized or denied on push: you are not logged in, or the token lacks write permission.

Save, load and clean up

Hour 2 · 52–60 · 8 min

podman save writes an image to a tar archive, and podman load reads it back. This is useful for moving images to an air-gapped host, or for a backup. The archive contains every layer, so treat it like the image itself:

podman save -o podman-app.tar localhost/podman-app:dev
podman rmi localhost/podman-app:dev
podman load -i podman-app.tar
podman images localhost/podman-app

Expected: load prints the image name it loaded, and images lists localhost/podman-app again.

Look at disk use before you clean up:

podman system df

Expected: a table with the images, containers and volumes, the space each uses, and how much of it is reclaimable.

Remove what is not needed, carefully:

podman image prune
podman image prune -a
podman system prune
  • image prune removes dangling images: untagged leftovers from rebuilds.
  • image prune -a removes every image that no container uses. Images you pulled but did not run go too.
  • system prune removes stopped containers, unused networks and dangling images. Add --volumes only when you are sure no volume holds data you need. Hour 4 covers volumes.
podman system reset removes all containers, images, volumes and networks for your user. It is for lab machines, not for hosts with data you have not backed up.

Common errors.

  • open podman-app.tar: no such file: load reads the file from the current folder. Use the full path.
  • Disk still full after prune: check podman system df for volumes, which prune leaves alone unless you pass --volumes.

Cheat sheet

Hour 2 ·
TaskCommand
Layers and metadatapodman history IMAGE, podman image inspect IMAGE
Build with a tagpodman build -t localhost/app:dev .
Build with a file namepodman build -f Containerfile -t app:dev .
Build without cachepodman build --no-cache -t app:dev .
Tag an imagepodman tag SRC DST
Log in, token on standard inputpodman login HOST --username USER --password-stdin
Push and pullpodman push HOST/NAME:TAG, podman pull HOST/NAME:TAG
Save and loadpodman save -o file.tar IMAGE, podman load -i file.tar
Disk usepodman system df
Remove dangling imagespodman image prune
Remove unused imagespodman image prune -a

Capstone lab: build, push, pull and clean up

Hour 2 ·

Goal. Take the sample service from source to a pulled image, the same path a pipeline follows.

cd podman-app
podman build -t localhost/podman-app:dev .
podman rm -f registry 2>/dev/null
podman run -d --name registry -p 127.0.0.1:5000:5000 docker.io/library/registry:2
podman tag localhost/podman-app:dev localhost:5000/podman-app:1.0.0
podman push --tls-verify=false localhost:5000/podman-app:1.0.0
podman rmi localhost:5000/podman-app:1.0.0
podman pull --tls-verify=false localhost:5000/podman-app:1.0.0
podman run -d --name svc -p 127.0.0.1:8000:8000 localhost:5000/podman-app:1.0.0
curl -s http://127.0.0.1:8000/health
podman rm -f svc
podman rm -f registry

Expected. The last curl prints {"status": "ok"}. The image that ran came from the registry, not from the local build.

Verify. podman images shows localhost:5000/podman-app with tag 1.0.0. podman ps -a lists no svc or registry after the cleanup.

Clean up. Remove the images with podman rmi once you no longer need them.

Final quiz

Hour 2

Sources and version notes