Skip to content
Merged
15 changes: 15 additions & 0 deletions labs/docker-multi-stage-copy-path/cloud-init.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions labs/docker-multi-stage-copy-path/lab.yaml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions labs/docker-multi-stage-copy-path/question.md
Original file line number Diff line number Diff line change
@@ -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 <image>`
- `sed -n '1,120p' Dockerfile`
19 changes: 19 additions & 0 deletions labs/docker-multi-stage-copy-path/solution.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions labs/docker-multi-stage-copy-path/solution.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions labs/docker-multi-stage-copy-path/verify.sh
Original file line number Diff line number Diff line change
@@ -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
Loading