Chapter 4

Containerize a Static nginx Page

Build a small immutable nginx image, run it locally, and inspect its HTTP health endpoint.

Just started Saved in this browser.

The first application stage is intentionally tiny: HTML and CSS served by nginx. Its Dockerfile copies known files into an official image and exposes port 8080.

Step by step

Download this chapter's files

Use your browser to download either ivia-chapter-04.tar.gz or ivia-chapter-04.zip. The archive is self-contained; you do not need this tutorial repository.

Open a terminal after the browser download:

cd ~/Downloads
tar -xzf ivia-chapter-04.tar.gz
cd ivia-chapter-04

All remaining relative paths in this chapter start from that extracted directory.

Do this

The challenge

Challenge: build simple-static:local, run it as a non-interactive container, and retrieve both the page and health endpoint.

Prepare

Prerequisites

  • Docker or a Docker-compatible CLI with a running engine.
  • The extracted Chapter 4 package, with your terminal in its top-level directory.
  • Port 8080 available locally.
  • No Kubernetes knowledge beyond Chapter 3 is required.
Outcome

Learning goals

  • Read a simple Dockerfile from top to bottom
  • Distinguish image build time from container runtime
  • Map a host port to a container port
  • Validate HTTP behavior and clean up safely
Step by step

Inspect the snapshot

find static-app -maxdepth 2 -type f -print
sed -n '1,120p' static-app/Dockerfile
sed -n '1,160p' static-app/nginx.conf

The COPY instructions make the page immutable: changing a source file does not change an already-built image. nginx listens on 8080, serves /usr/share/nginx/html, and returns ok from /healthz.

Step by step

Build and run

docker build \
  -t simple-static:local \
  static-app

docker image inspect simple-static:local --format '{{.RepoTags}}'
docker run --rm --name simple-static -d -p 8080:8080 simple-static:local

Validate from another terminal:

curl --fail http://localhost:8080/
curl --fail http://localhost:8080/healthz
docker logs simple-static

Open http://localhost:8080 in a browser. When finished:

docker stop simple-static

Because the container used --rm, stopping it also removes the container. The image remains available for Minikube in the next chapter.

Step by step

What each option means

  • -t simple-static:local gives the image a readable repository and tag.
  • -d runs in the background.
  • -p 8080:8080 forwards host port 8080 to container port 8080.
  • --rm removes the stopped container, keeping your environment tidy.
  • curl --fail returns a nonzero status for HTTP errors, which makes validation reliable.
Verify

Expected validation

The page response should contain Hello from Kubernetes!; the health response should be exactly ok. docker ps should show 0.0.0.0:8080->8080/tcp while the container runs. After stopping, this should return no row:

docker ps --filter name=simple-static
Troubleshoot

Troubleshooting and common pitfalls

  • Port already allocated: find the existing owner or map another host port, for example -p 8081:8080, then use http://localhost:8081.
  • Build context error: run from the extracted chapter directory and keep static-app as the final argument.
  • Page changes do not appear: rebuild the image; source is copied at build time. Browser hard-refresh if it cached the old page.
  • Container exits immediately: run docker logs simple-static before cleanup, or temporarily omit --rm so the stopped container remains inspectable.
  • Apple Silicon warning about platform: use a multi-architecture official nginx image as provided; do not force amd64 unless your deployment requires it.
  • Using localhost inside a container: there it means that container, not your host or another container.

You have packaged a reproducible web server. The same image can now move from your laptop into Kubernetes without changing its contents.

Reference

Official references

Just started Saved in this browser.