# Exercise 1 — Container Lifecycle Lab

<!--
STEP 1: Run the setup command below to start a named, detached container
        from nginx:alpine, publishing container port 80 to host port 8088.
STEP 2: Fill in the TODO commands in "Your Commands" — one per line, the
        exact `docker` command that satisfies the requirement in the comment.
STEP 3: Save the self-check script at the bottom as `check.sh`, run
        `bash check.sh`, and make sure every line prints PASS.
STEP 4: Answer the two short reasoning questions at the end in your own words.

Requires a working Docker installation (Docker Desktop or a Linux Docker
daemon). Nothing here can be verified without actually running Docker.
-->

## Setup

```bash
docker run -d --name fundamentals-lab -p 8088:80 nginx:alpine
```

## Your Commands

Write the exact `docker` command for each requirement. Replace `# TODO` with
the real command — do not just describe it in prose.

```bash
# TODO 1: Confirm the container is running and show its status/ports column.


# TODO 2: Stream the container's logs without exiting (follow mode).
#         (Press Ctrl+C after you see at least one line, then move on.)


# TODO 3: Open an interactive shell inside the running container.
#         (nginx:alpine has `sh`, not `bash` — use the shell that exists.)


# TODO 4: From your HOST machine (not inside the container), confirm nginx
#         is reachable on the port you published — use curl or a browser.


# TODO 5: Inspect the container's full JSON metadata and find the field
#         that shows its assigned IP address inside Docker's default
#         bridge network. Name the exact top-level JSON path
#         (e.g. NetworkSettings.Networks.bridge.IPAddress) as a comment.


# TODO 6: Stop the container gracefully (not `docker kill`).


# TODO 7: Remove the stopped container.


# TODO 8: Remove the nginx:alpine image itself.
```

## Self-Check

Save this as `check.sh` next to your answers and run it with `bash check.sh`
at each stage the comments indicate. It only checks facts about your running
Docker environment, not the prose above — it can't grade your TODO answers,
but it verifies you actually performed the underlying actions.

```bash
#!/usr/bin/env bash
# Run this AFTER Setup and BEFORE you stop/remove the container (checks 1-3),
# then run it again AFTER Step 6-8 (checks 4-5) — the script tells you which
# phase you're in based on what it finds.
set -uo pipefail
pass=0
fail=0

check() {
  local desc="$1"
  local cond="$2"
  if eval "$cond"; then
    echo "PASS: $desc"
    pass=$((pass + 1))
  else
    echo "FAIL: $desc"
    fail=$((fail + 1))
  fi
}

if docker inspect fundamentals-lab >/dev/null 2>&1; then
  check "container 'fundamentals-lab' exists" "true"
  check "container is running" \
    "[ \"\$(docker inspect -f '{{.State.Running}}' fundamentals-lab)\" = 'true' ]"
  check "container publishes host port 8088" \
    "docker port fundamentals-lab | grep -q '0.0.0.0:8088\\|:::8088'"
  check "nginx responds on published port" \
    "curl -sf -o /dev/null http://localhost:8088"
else
  check "container 'fundamentals-lab' was removed (post-cleanup phase)" "true"
  check "image nginx:alpine was removed" \
    "! docker image inspect nginx:alpine >/dev/null 2>&1"
fi

echo "---"
echo "$pass passed, $fail failed"
[ "$fail" -eq 0 ] && echo "PASS" || echo "FAIL"
```

## Reasoning Questions

1. After `docker rm fundamentals-lab`, is the image `nginx:alpine` still on
   disk? Why or why not, in terms of what an image vs. a container actually
   is?
2. If you ran `docker run -d --name fundamentals-lab -p 8089:80 nginx:alpine`
   a second time (different host port, same container name) while the first
   was still running, what would happen, and why?
