Docker Image Management Lab

Lab 1: The Smallest Website

  1. Create a folder: mkdir my-site and go inside it.
  2. Create index.html: Write <h1>Hello World</h1> in it.
  3. Create Dockerfile:DockerfileFROM nginx:alpine COPY index.html /usr/share/nginx/html/index.html
  4. Build: docker build -t tiny-site:v1 .
  5. Run: docker run -d -p 9999:80 tiny-site:v1
  6. Test: Open http://localhost:9999.

Lab 2: The History Lesson

  1. Run: docker history tiny-site:v1
  2. Observe: Look at the “SIZE” column. You will see the base nginx layers are large, and your COPY layer is very small (just bytes).
  3. Learn: This proves that your changes are just a tiny layer on top of the base.

Lab 1: The “Bloatware vs. Slim” Challenge

Goal: See the massive difference a good base image makes.

  1. Create a folder: mkdir compare-images and go inside.
  2. Create Dockerfile.fat:DockerfileFROM ubuntu:latest RUN apt-get update && apt-get install -y python3 CMD ["python3", "--version"]
  3. Build Fat Image: docker build -f Dockerfile.fat -t my-python:fat .
  4. Create Dockerfile.slim:DockerfileFROM python:alpine CMD ["python", "--version"]
  5. Build Slim Image: docker build -f Dockerfile.slim -t my-python:slim .
  6. Compare Sizes:docker images | grep my-python
    • Result: You will likely see fat is ~100MB-500MB, while slim is ~50MB.
    • Lesson: Always choose specific Alpine or Slim images, not generic OS images like Ubuntu/CentOS.

Lab 2: Multi-Stage Builds (The Professional Standard)

Goal: Compile code in one container, but ship ONLY the binary in the final image.

  1. Create a Go file (main.go): (Even if you don’t know Go, just copy this).Gopackage main import "fmt" func main() { fmt.Println("Hello from a Tiny Container!") }
  2. Create Dockerfile:Dockerfile# Stage 1: The Builder (Has all the heavy tools) FROM golang:1.21 AS builder WORKDIR /app COPY main.go . RUN go build -o myapp main.go # Stage 2: The Runner (Has almost nothing) FROM alpine:latest WORKDIR /root/ # Copy ONLY the compiled file from the builder stage COPY --from=builder /app/myapp . CMD ["./myapp"]
  3. Build: docker build -t my-go-app:v1 .
  4. Check Size:docker images my-go-app:v1
    • Result: The image will be tiny (maybe 10MB). If you used the standard way, it would be 800MB+!

Lab 3: The Private Registry (Simulating Corporate Environment)

Goal: Run your own “Docker Hub” locally and push images to it.

  1. Start a Registry: docker run -d -p 5000:5000 --name local-registry registry:2 (You now have a private app store running on port 5000).
  2. Tag an Image for Localhost: docker tag my-go-app:v1 localhost:5000/my-secret-app:v1
  3. Push to Local Registry: docker push localhost:5000/my-secret-app:v1
  4. Verify: Delete the image from your main list: docker rmi localhost:5000/my-secret-app:v1 Now pull it back: docker pull localhost:5000/my-secret-app:v1
  5. Cleanup: docker rm -f local-registry

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top