#!/usr/bin/env bash # examine result # docker run --rm -it -v webawesome-3.2.1:/volume alpine sh set -euo pipefail # strict error handling # Usage message usage() { echo "Usage: $0 [source_dir]" echo " volume_name Name of the Docker volume to create/populate" echo " source_dir Source directory (default: current directory)" exit 1 } # Check args [[ $# -lt 1 ]] && usage VOLUME_NAME="$1" SOURCE_DIR="${2:-.}" # Resolve source dir to absolute path (handles relative paths, symlinks via readlink) if ! SOURCE_DIR=$(cd "$SOURCE_DIR" && pwd); then echo "❌ Error: Source directory '$2' does not exist or is not readable." >&2 exit 1 fi echo "📦 Populating Docker volume '$VOLUME_NAME' from: $SOURCE_DIR" # Create the volume (safe: no-op if exists) docker volume create "$VOLUME_NAME" >/dev/null 2>&1 || true # Use Alpine Linux container to copy contents into the volume # - --rm: auto-cleanup after exit # - read-only source for safety # - cp -a preserves all metadata (symlinks, perms, timestamps) # - /source/. → copies *contents* of source dir into target root docker run --rm \ -v "$SOURCE_DIR:/source:ro" \ -v "$VOLUME_NAME:/target" \ alpine:latest \ sh -c 'cp -a /source/. /target/' || { echo "❌ Error copying files. Is Docker running?" >&2 exit 1 } echo "✅ Volume '$VOLUME_NAME' is now populated from '$SOURCE_DIR'"