Build Your Own Web Image · Docker
Capture changes in an image instead of manually editing a running container.
Learn with progress and quizzesFirst, understand the concepts.
Build context
The final docker build path defines files available to COPY. Exclude unnecessary files with .dockerignore and keep credentials out of images.
Build time and runtime
FROM and COPY define image contents. CMD supplies the default startup command, unlike RUN instructions executed during a build.
Tags and ports
A tag is a movable name, not an immutable identity. Publishing 127.0.0.1:18081:80 exposes container port 80 only on the Docker host’s loopback address.
Why it matters
CACHED in a second build indicates reused work. Separating stable dependencies from frequently changed sources makes the build sequence easier to reason about.
Work through the reasoning
Context and layers
COPY sources come from the build context. .dockerignore reduces unnecessary context files. Changes to earlier inputs or steps affect reuse of later work.
Verify the running result
Building prepares files and default execution settings. Publish the host port with -p and check HTTP output to verify what the running container actually serves.
Learn by doing.
Use Docker Engine or Docker Desktop with Linux containers, Compose v2, a POSIX shell, and curl on a personal practice host. Clean up only the study-prefixed resources you create. Pulling images requires network access.
Create HTML and a Dockerfile in a new temporary directory, then build study-web:v1. If your Docker daemon is remote, run curl on that host.
study_build_dir=$(mktemp -d)
cd "$study_build_dir"
printf '<h1>Study build v1</h1>
' > index.html
cat > Dockerfile <<'DOCKERFILE'
FROM nginx:stable-alpine
COPY index.html /usr/share/nginx/html/index.html
DOCKERFILE
printf '.git
*.key
' > .dockerignore
docker build -t study-web:v1 .
docker run -d --name study-built-web -p 127.0.0.1:18081:80 study-web:v1
curl --retry 10 --retry-all-errors --retry-delay 1 http://127.0.0.1:18081
docker image inspect study-web:v1 --format '{{.Id}}'How to check your result
Expect Study build v1 from curl and an image ID from inspect. Changing the HTML and rebuilding does not replace an existing running container. If port 18081 is busy, check its owner or choose another port.
Clean up practice resources
Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.
docker stop study-built-web
docker rm study-built-web
docker image rm study-web:v1Apply what you learned
You edited the web file but still see the old page. What should happen next?
Reveal the reasoning after trying
Build the updated image and create a container using it. Restarting an existing container does not replace its image. Compare actual image IDs as well as tags to narrow the issue.
Ready to check your understanding?
What defines the local files available to COPY?
- The container name
- The build context
- Container logs
- Every file on the host
Reveal the reasoning after trying
The build context
COPY uses files from the build context. Tags and container names do not define file access.
Official documentation · Build Your Own Web Image · Further reading · official documentation