diff --git a/labs/docker-multi-stage-copy-path/cloud-init.yaml b/labs/docker-multi-stage-copy-path/cloud-init.yaml new file mode 100644 index 0000000..c120464 --- /dev/null +++ b/labs/docker-multi-stage-copy-path/cloud-init.yaml @@ -0,0 +1,15 @@ +#cloud-config +packages: + - docker.io +runcmd: + - mkdir -p /opt/brokenops/docker-multi-stage-copy-path + - | + cat <<'EOF' > /opt/brokenops/docker-multi-stage-copy-path/Dockerfile + FROM alpine:3.19 AS builder + RUN mkdir -p /out && echo 'BrokenOps multi-stage build' > /out/app.txt + + FROM alpine:3.19 + COPY --from=builder /out/app.tx /app/app.txt + CMD ["cat", "/app/app.txt"] + EOF + - chown -R root:root /opt/brokenops/docker-multi-stage-copy-path diff --git a/labs/docker-multi-stage-copy-path/lab.yaml b/labs/docker-multi-stage-copy-path/lab.yaml new file mode 100644 index 0000000..1a6f94b --- /dev/null +++ b/labs/docker-multi-stage-copy-path/lab.yaml @@ -0,0 +1,13 @@ +id: "docker-multi-stage-copy-path" +name: "Docker Multi-Stage COPY Path" +category: "docker" +difficulty: "intermediate" +description: + summary: "A multi-stage Docker build fails because the final stage copies from the wrong artifact path." +vm: + name: "docker-copy-lab" + memory: 2048 + cpu: 2 + disk: "10G" +cloud_init: "cloud-init.yaml" +verify_script: "verify.sh" diff --git a/labs/docker-multi-stage-copy-path/question.md b/labs/docker-multi-stage-copy-path/question.md new file mode 100644 index 0000000..0e55a6e --- /dev/null +++ b/labs/docker-multi-stage-copy-path/question.md @@ -0,0 +1,10 @@ +### Scenario +A container image stopped building after a refactor changed the artifact path in a multi-stage Dockerfile. + +### Objective +Fix the Dockerfile so the final image can be built and run successfully again. + +### Useful Commands +- `docker build .` +- `docker run --rm ` +- `sed -n '1,120p' Dockerfile` diff --git a/labs/docker-multi-stage-copy-path/solution.md b/labs/docker-multi-stage-copy-path/solution.md new file mode 100644 index 0000000..2ecf427 --- /dev/null +++ b/labs/docker-multi-stage-copy-path/solution.md @@ -0,0 +1,19 @@ +### The Issue +The final Docker stage is copying the built artifact from the wrong path. The builder creates `/out/app.txt`, but the final stage tries to copy `/out/app.tx`. + +### Step-by-Step Fix + +1. **Inspect the Dockerfile**: + Read the multi-stage build and confirm the artifact name in the builder stage. + +2. **Correct the `COPY` source path**: + Update the final stage so it copies `/out/app.txt` into the runtime image. + +3. **Rebuild the image**: + Build the image again and make sure the Docker build completes without errors. + +4. **Run the container**: + Start the built image and confirm it prints `BrokenOps multi-stage build`. + +5. **Verify the fix**: + Once the build and run both succeed, the lab is solved. diff --git a/labs/docker-multi-stage-copy-path/solution.sh b/labs/docker-multi-stage-copy-path/solution.sh new file mode 100755 index 0000000..17400b5 --- /dev/null +++ b/labs/docker-multi-stage-copy-path/solution.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -euo pipefail +cd /opt/brokenops/docker-multi-stage-copy-path +sed -i 's/app.tx/app.txt/' Dockerfile diff --git a/labs/docker-multi-stage-copy-path/verify.sh b/labs/docker-multi-stage-copy-path/verify.sh new file mode 100755 index 0000000..490522a --- /dev/null +++ b/labs/docker-multi-stage-copy-path/verify.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -euo pipefail +cd /opt/brokenops/docker-multi-stage-copy-path +if ! docker build -t brokenops-multi-stage-copy . >/tmp/brokenops-multi-stage-build.log 2>&1; then + cat /tmp/brokenops-multi-stage-build.log + exit 1 +fi +if ! docker run --rm brokenops-multi-stage-copy sh -c 'test "$(cat /app/app.txt)" = "BrokenOps multi-stage build"'; then + docker run --rm brokenops-multi-stage-copy cat /app/app.txt || true + exit 1 +fi +echo "SUCCESS: The multi-stage build produces the expected artifact." +exit 0