Skip to content

Latest commit

 

History

History
122 lines (83 loc) · 4.57 KB

File metadata and controls

122 lines (83 loc) · 4.57 KB

Exercises for Containerisation Workshop

Create Dockerfiles for these applications

Pre-requisites

  • Install Docker Desktop or Rancher Desktop (OSS)
  • Make sure docker -v works from your terminal.

Docs

Have these documentation sites in hand when doing the assignments:

Assignments

  1. Containerize each app, starting with the lang/tech you feel most comfortable with. See TASKS further down in this readme.
  2. Try to first create a single-stage Dockerfile to build and run the app.
  • It must automatically start the application when using docker run <image-name>
  1. Then split your Dockerfile into a build stage and a runner/app stage.

Solutions

If you're completely stuck or just want to compare, solutions can be found on the solutions branch.

Useful commands

Building

  • Build: docker build <./context/dir> -f ./path/to/Dockerfile -t <name:tag>
    • Serialized: DOCKER_BUILDKIT=0 docker build ... (debug each layer/cmd, by using printed sha as image-tag)
  • Images & their sizes:
    • Show local repo: docker image ls
    • Inspect an image: docker image inspect <name:tag>
  • Re-tag: docker tag <source-name:tag> <dest-name:tag>

Running

  • What's running? docker ps
  • Run: docker run <image-tag> (runs interactively until done or ctrl+c)
  • Keep running: docker run --detached <image-tag> (runs in background until done)
  • Debug: docker run -it --entrypoint /bin/bash <image-tag>

1. ubuntu-debugger

Simple image that extends Ubuntu 24.04 distro with some extra network debugging tools. Useful to understand base-images and layering

TASK

  1. Start with FROM ubuntu24.04 and then make sure curl and dnsutils are installed in the container image.
  2. Start and execute into the container image, then try using the curl and nslookup tools.

2. static-web-app

Static nginx-based web-app which hosts static HTML files.

TASK

  1. Create a static http web-app that hosts our static replacement index.html file.
  2. You can use nginx, python -m http.server or any other way to host the static html/index.html file via HTTP on an exposed port.

3. micro-go-app

Simple scratch-based go app that just outputs the contents of a .txt file

TASK

  1. This task is purely educational. Inspect the Dockerfile and learn about scratch
  2. Build the container image and check its disk-size! docker images | grep <image-tag>

java-hello-world

Simple hello-world app in Java.

TASK

  1. Containerize this little Java hello-world app, make it start the Hello.java file.
  2. Run the container image and change who it says hello to at runtime.
  3. Make the container print the contents of a local file by volume mapping it into the container.

java-hello-world-pom

TASK

  1. Containerize the maven-based Java hello-world app.
  2. Run the container image and change who it says hello to at runtime.
  3. Make the container print the contents of a local file by volume mapping it into the container.

dotnet-hello-world

Simple hello-world app in .NET 10

Create a Dockerfile for the app and test it!

TASK

  1. Follow Microsoft's guide on containerizing dotnet apps
  2. See https://learn.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows&pivots=dotnet-10-0

Best practices

  • Minimize layers by combining related commands.
  • Split Dockerfile into build and runtime stages! (massively reduces image size)
  • Use .dockerignore to reduce the build-context and image size.
  • Pin versions! Use FROM alpine:3.19.0 rather than just alpine or alpine:latest.
    • Use of :stable tags might be a good idea if available.
  • Add metadata to your container images!
    LABEL org.opencontainers.image.source="https://github.com/org/repo"
    LABEL org.opencontainers.image.version="1.0.0"
    LABEL org.opencontainers.image.description="My custom base image"
    

Bonus assignments

  1. Use docker compose up -d to build and launch all our containers (in the same virtual network).
  2. Try executing into on of the running containers (docker compose exec -it <service-name> /bin/bash).
  3. Try to fetch our HTML website from static-web-app via the ubuntu-debugger container.
  4. Try fetching logs from our static-web-app via cmd docker compose logs -f <service-name>, can you see the fetch?
  5. Try reducing some of the container image sizes! Check with docker images.