Sample — Code block conventions¶
Approved convention
This page is the canonical sample for lab code blocks. Terminal chrome (dark panel) versus file chrome (green filename bar) is the site-wide standard. Hard-refresh if styles look stale.
What to look for¶
| Convention | How it appears |
|---|---|
| File to create | Light panel + green filename bar (title="main.tf") |
| Commands to run | Dark terminal chrome (title="Terminal") — not the same as file blocks |
| Expected output | !!! example "Expected output" admonition |
| Long files | Optional linenums="1" (shown once below) |
| Always tagged | No bare fences |
Hands-on Lab¶
Objective¶
Apply a tiny Docker Terraform stack and prove the container with CLI evidence — using the new code-block conventions.
Prerequisites¶
- Terraform CLI ≥ 1.5
- Docker Engine running
Lab environment¶
Runtime: local Docker Engine + kreuzwerker/docker provider.
Real-world scenario¶
A platform engineer is onboarding a disposable lab pattern: create config as files (not heredocs), run a short terminal session, and prove success with a clear expected-output panel — the same shape learners should see in production interview prep labs.
Step-by-step tasks¶
Task 1 – Author provider and network¶
Create the provider pin and a bridge network.
terraform {
required_version = ">= 1.5.0"
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0"
}
}
}
provider "docker" {}
resource "docker_network" "lab" {
name = "rebash-sample-net"
driver = "bridge"
}
resource "docker_image" "nginx" {
name = "nginx:1.25-alpine"
keep_locally = true
}
resource "docker_container" "web" {
name = "rebash-sample-web"
image = docker_image.nginx.image_id
networks_advanced {
name = docker_network.lab.name
}
ports {
internal = 80
external = 18080
}
labels {
label = "rebash.sample"
value = "code-blocks"
}
}
output "container_name" {
value = docker_container.web.name
}
Initialise and apply:
cd ~/rebash-labs/sample-code-blocks
terraform init
terraform apply -auto-approve
docker ps --filter name=rebash-sample-web --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' | tee docker-ps.txt
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:18080/ | tee http-code.txt
grep -q rebash-sample-web docker-ps.txt
grep -q 200 http-code.txt
Expected output
terraform applycreates the network andrebash-sample-webdocker-ps.txtlistsrebash-sample-webas Uphttp-code.txtcontains200
Task 2 – Prove labels (interview-style evidence)¶
Inspect the container label set by Terraform:
cd ~/rebash-labs/sample-code-blocks
docker inspect -f '{{index .Config.Labels "rebash.sample"}}' rebash-sample-web | tee label.txt
grep -q code-blocks label.txt
terraform output -raw container_name | tee output-name.txt
grep -q rebash-sample-web output-name.txt
Expected output
label.txtiscode-blocksoutput-name.txtisrebash-sample-web
Task 3 – Optional: long file with line numbers¶
Use line numbers only when a file is long enough that reviewers discuss specific lines (example shape — do not require this file for the lab):
| compose.example.yaml | |
|---|---|
Validation steps¶
-
docker psshowsrebash-sample-web - HTTP check returns
200 - Label and Terraform output match
- Filename titles appear above file fences; Terminal title appears on command fences
- Expected output uses green/example admonitions, not plain bold text alone
Common errors and fixes¶
| Error | Cause | Fix |
|---|---|---|
| Cannot connect to Docker | Daemon not running | Start Docker Desktop / dockerd |
| Port 18080 in use | Another local service | Change external port in main.tf and re-apply |
| Provider not found | Skipped init | Run terraform init again |
Challenge exercise¶
Add an output network_name and prove it with terraform output plus docker network ls.
Learning outcomes¶
- File fences carry
title="filename"so copy targets are obvious - Terminal fences stay short and labelled
- Success criteria sit in an Expected output admonition
- Lab still does real apply / proof / cleanup
Cleanup¶
cd ~/rebash-labs/sample-code-blocks
terraform destroy -auto-approve
rm -f docker-ps.txt http-code.txt label.txt output-name.txt
Expected output
Terraform reports destroy complete; docker ps -a --filter name=rebash-sample-web is empty.
Authoring cheat sheet (for reviewers)¶
Create `main.tf`:
```hcl title="main.tf"
resource "docker_network" "lab" {
name = "rebash-sample-net"
}
```
``` {.bash .ra-terminal title="Terminal"}
terraform apply -auto-approve
```
!!! example "Expected output"
Apply succeeds; resource exists.
Do not use **Expected output:** as the only success signal once this convention is approved.
Do not put multi-line files inside cat <<EOF heredocs.