Containerize a Static nginx Page
Build a small immutable nginx image, run it locally, and inspect its HTTP health endpoint.
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.
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.
The challenge
Challenge: build simple-static:local, run it as a non-interactive container, and retrieve both the page and health endpoint.
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.
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
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.
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.
What each option means
-t simple-static:localgives the image a readable repository and tag.-druns in the background.-p 8080:8080forwards host port 8080 to container port 8080.--rmremoves the stopped container, keeping your environment tidy.curl --failreturns a nonzero status for HTTP errors, which makes validation reliable.
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
Troubleshooting and common pitfalls
- Port already allocated: find the existing owner or map another host port, for example
-p 8081:8080, then usehttp://localhost:8081. - Build context error: run from the extracted chapter directory and keep
static-appas 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-staticbefore cleanup, or temporarily omit--rmso the stopped container remains inspectable. - Apple Silicon warning about platform: use a multi-architecture official nginx image as provided; do not force
amd64unless your deployment requires it. - Using
localhostinside 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.