DOCKER · Investigation · 8 min

Read logs after a container exits

Capture stdout from a short-lived named container.

Not completed

Your task

Run the prepared image as the named container and save its logs into captured.log after it exits.

What you’re aiming for

  • The stopped container has exit code 0.
  • captured.log contains INFO boot and ERROR synthetic.

1. Prepare your practice folder

Use a running Docker engine in Linux container mode. The setup commands below use a Bash-compatible shell: Git Bash on Windows, or Bash on Linux/macOS.

Create a new, empty folder for this exercise. If that name already exists, choose another new name. Continue only after you’re inside the new folder.

NEW PRACTICE FOLDER
mkdir opsrung-docker-capture-container-logs && cd opsrung-docker-capture-container-logs

Run these setup commands in that folder:

SETUP — RUN ONCE
cat > Dockerfile <<'DOCKERFILE'
FROM alpine:3.22
USER 65534:65534
CMD ["sh","-c","printf 'INFO boot\\nERROR synthetic\\n'"]
DOCKERFILE
docker build -t opsrung-capture-container-logs:practice .

Docker may download the official Alpine image and, for the web task, its HTTP package. If an example container name already exists, use a different name consistently. If port 8088 is occupied, choose another free local port.

2. Try the task

Work toward the goal above. Start by inspecting the files or resources, then use a hint if you need one.

Hint 1

Do not use --rm if you want to inspect this stopped container.

Hint 2

docker logs reads the recorded stdout/stderr.

Hint 3

Redirect docker logs output to captured.log.

3. Review your result

Use these commands to look at your result and compare it with the goals. The website does not run or verify them.

REVIEW IN YOUR TERMINAL
cat captured.log
docker inspect opsrung-capture-container-logs --format "{{.State.ExitCode}}"

What you should see

  • The stopped container has exit code 0.
  • captured.log contains INFO boot and ERROR synthetic.

If your result differs: Compare the actual output with each goal. Keep your work inside this exercise’s folder and use only its named resources.

Compare with one possible solution
ONE POSSIBLE SOLUTION
docker run --name opsrung-capture-container-logs --network none --read-only --cap-drop ALL --security-opt no-new-privileges opsrung-capture-container-logs:practice
docker logs opsrung-capture-container-logs > captured.log

Other approaches are fine if you reach the same goal. Opening this solution does not mark the exercise complete.

Clean up this exercise

Only remove the example resources you created. Volume cleanup deletes that exercise’s saved sample data. Keep anything you need.

OPTIONAL CLEANUP
docker stop opsrung-capture-container-logs
docker rm opsrung-capture-container-logs
docker image rm opsrung-capture-container-logs:practice

Finished this exercise?

You decide when you’re done. Your choice is saved in this browser, and you can change it later.