Skip to content

Instantly share code, notes, and snippets.

@spinningcat
Created March 9, 2025 12:09
Show Gist options
  • Save spinningcat/5ed665eb8d37953f4f1d41b6e3caba04 to your computer and use it in GitHub Desktop.
Save spinningcat/5ed665eb8d37953f4f1d41b6e3caba04 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Exit on error
set -e
command -v docker >/dev/null 2>&1 || { echo "Docker is not installed. Aborting." >&2; exit 1; }
command -v docker-compose >/dev/null 2>&1 || { echo "Docker Compose is not installed. Aborting." >&2; exit 1; }
VERSION=${VERSION:-"1.0.0"} # Use environment variable if set, otherwise default
DOCKER_REGISTRY=${DOCKER_REGISTRY:-""} # Leave empty for local use
IMAGE_PREFIX="myapp" # Made-up prefix for local use
COMPOSE_FILE=${COMPOSE_FILE:-"docker-compose.yml"} # Allow specifying compose file
declare -A DOCKERFILES=(
["Dockerfile.debian"]="debian-image"
["Dockerfile.minio"]="minio-image"
["Dockerfile.node"]="node-image"
["Dockerfile.sqlite"]="sqlite-image"
)
build_and_tag() {
local dockerfile=$1
local image_name=$2
local tag=$3
local full_image_name="$IMAGE_PREFIX/$image_name:$tag"
echo "Building $image_name from $dockerfile..."
docker build -f "$dockerfile" -t "$full_image_name" .
echo "Tagging $full_image_name as $IMAGE_PREFIX/$image_name:latest"
docker tag "$full_image_name" "$IMAGE_PREFIX/$image_name:latest"
}
# Trap errors
trap 'echo "An error occurred. Exiting..."; exit 1;' ERR
for dockerfile in "${!DOCKERFILES[@]}"; do
build_and_tag "$dockerfile" "${DOCKERFILES[$dockerfile]}" "$VERSION"
done
# Optionally push images to Docker registry
if [ "$1" == "push" ]; then
if [ -z "$DOCKER_REGISTRY" ]; then
echo "DOCKER_REGISTRY is not set. Skipping push."
else
echo "Pushing images to Docker registry..."
for image_name in "${DOCKERFILES[@]}"; do
docker push "$DOCKER_REGISTRY/$IMAGE_PREFIX/$image_name:$VERSION"
docker push "$DOCKER_REGISTRY/$IMAGE_PREFIX/$image_name:latest"
done
fi
fi
# Deploy with docker-compose
echo "Deploying with docker-compose..."
docker-compose -f "$COMPOSE_FILE" up -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment