From dd1d9053717821ebe85aec3bd67578f17d1c155a Mon Sep 17 00:00:00 2001 From: Frank Engel Date: Wed, 5 Aug 2026 12:35:36 +0200 Subject: [PATCH] =?UTF-8?q?ex12a:=20Containerfile=20Schritt=20f=C3=BCr=20S?= =?UTF-8?q?chritt=20aufbauen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hello.sh, Containerfile (Referenz) und commands.txt mit allen 6 Schritten inkl. Layer-Bonus. Co-Authored-By: Claude Sonnet 4.6 --- .../ex12a-containerfile-steps/Containerfile | 12 +++ .../ex12a-containerfile-steps/commands.txt | 75 +++++++++++++++++++ .../ex12a-containerfile-steps/hello.sh | 5 ++ 3 files changed, 92 insertions(+) create mode 100644 listings/exercises/ex12a-containerfile-steps/Containerfile create mode 100644 listings/exercises/ex12a-containerfile-steps/commands.txt create mode 100644 listings/exercises/ex12a-containerfile-steps/hello.sh diff --git a/listings/exercises/ex12a-containerfile-steps/Containerfile b/listings/exercises/ex12a-containerfile-steps/Containerfile new file mode 100644 index 0000000..486d331 --- /dev/null +++ b/listings/exercises/ex12a-containerfile-steps/Containerfile @@ -0,0 +1,12 @@ +FROM debian:12 + +RUN apt-get update && apt-get install -y curl + +WORKDIR /app + +COPY hello.sh . +RUN chmod +x hello.sh + +ENV NAME=Trainee + +CMD ["./hello.sh"] diff --git a/listings/exercises/ex12a-containerfile-steps/commands.txt b/listings/exercises/ex12a-containerfile-steps/commands.txt new file mode 100644 index 0000000..3e41c46 --- /dev/null +++ b/listings/exercises/ex12a-containerfile-steps/commands.txt @@ -0,0 +1,75 @@ +# Exercise 12a — Build a Containerfile step by step +# +# Start with an empty Containerfile and add one instruction at a time. +# After each step: podman build -t myapp:latest . +# Then inspect the result before moving on. + +cd listings/exercises/ex12a-containerfile-steps/ + +# ----------------------------------------------------------------------- +# Step 1 — Base image +# ----------------------------------------------------------------------- +# Containerfile: +# FROM debian:12 + +podman build -t myapp:latest . +podman images myapp:latest # ~121 MB + +# ----------------------------------------------------------------------- +# Step 2 — Install software (creates a new layer) +# ----------------------------------------------------------------------- +# Add to Containerfile: +# RUN apt-get update && apt-get install -y curl + +podman build -t myapp:latest . +podman images myapp:latest # ~159 MB — layer is visible + +# ----------------------------------------------------------------------- +# Step 3 — Working directory +# ----------------------------------------------------------------------- +# Add to Containerfile: +# WORKDIR /app + +podman build -t myapp:latest . +podman run --rm myapp:latest pwd # prints /app + +# ----------------------------------------------------------------------- +# Step 4 — Copy your script into the image +# ----------------------------------------------------------------------- +# Add to Containerfile: +# COPY hello.sh . +# RUN chmod +x hello.sh + +podman build -t myapp:latest . +podman run --rm myapp:latest ls -la # hello.sh is there + +# ----------------------------------------------------------------------- +# Step 5 — Environment variable +# ----------------------------------------------------------------------- +# Add to Containerfile: +# ENV NAME=Trainee + +podman build -t myapp:latest . +podman run --rm myapp:latest env | grep NAME + +# ----------------------------------------------------------------------- +# Step 6 — Start command +# ----------------------------------------------------------------------- +# Add to Containerfile: +# CMD ["./hello.sh"] + +podman build -t myapp:latest . +podman run --rm myapp:latest # uses ENV NAME=Trainee +podman run --rm -e NAME=Frank myapp:latest # override at runtime + +# ----------------------------------------------------------------------- +# Bonus: layers — chained vs. separate RUN +# ----------------------------------------------------------------------- +# Two separate RUN = two layers: +# RUN apt-get update +# RUN apt-get install -y curl +# +# One chained RUN = one layer: +# RUN apt-get update && apt-get install -y curl + +podman inspect myapp:latest --format '{{len .RootFS.Layers}} layers' diff --git a/listings/exercises/ex12a-containerfile-steps/hello.sh b/listings/exercises/ex12a-containerfile-steps/hello.sh new file mode 100644 index 0000000..5a0946b --- /dev/null +++ b/listings/exercises/ex12a-containerfile-steps/hello.sh @@ -0,0 +1,5 @@ +#!/bin/bash +echo "Hello, ${NAME:-World}!" +echo "Hostname: $(hostname)" +echo "Date: $(date)" +curl --version | head -1