Scenario Docker Intermediate

Container Status Report

Create a read-only Bash report of container state.

1 min read
3 Steps
2 Services Used
~30 min Duration
Intermediate Difficulty
The Situation

Docker automation with Bash

Task

Generate a quick inventory for troubleshooting without changing containers.

Prerequisites

Bash and a working Docker CLI. Save this as container-report.sh.

#!/usr/bin/env bash
set -euo pipefail

if ! docker info >/dev/null 2>&1; then
    printf 'Cannot reach the Docker daemon. Check the active context.\n' >&2
    exit 1
fi

printf 'Context: '
docker context show
printf '\nContainers:\n'
docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
printf '\nRunning container resource usage:\n'
docker stats --no-stream

Run

bash container-report.sh

How It Works

The daemon check fails early when Docker is unavailable. The formatted list includes stopped containers, while stats covers running containers. Quoting preserves the Go template passed to Docker.

Verification

Run once with a practice container running and again after stopping it. The container should remain in the inventory and disappear from running resource usage.

Interview Follow-up

How would you produce JSON for another program? Why is parsing a human-readable status string fragile?


Docker Roadmap

Services Used
DockerBash

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form