Skip to content

Instantly share code, notes, and snippets.

@greenido
Created February 3, 2025 19:28
Show Gist options
  • Save greenido/e5484050ae5a6caeff30aa1066eb1436 to your computer and use it in GitHub Desktop.
Save greenido/e5484050ae5a6caeff30aa1066eb1436 to your computer and use it in GitHub Desktop.
A Docker Image Build and EC2 Copy Script - v0.5
#!/bin/bash
#
# A Docker Image Build and EC2 Copy Script for macOS
# Updated: 4/4/2024
#
# Logging function
log() {
local log_level="${1}"
local message="${2}"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Color codes for different log levels
local COLOR_OFF="\033[0m"
local GREEN="\033[0;32m"
local YELLOW="\033[0;33m"
local RED="\033[0;31m"
local BLUE="\033[0;34m"
case "${log_level}" in
"INFO")
echo -e "${GREEN}[${timestamp}] [INFO]${COLOR_OFF} ${message}"
;;
"WARN")
echo -e "${YELLOW}[${timestamp}] [WARN]${COLOR_OFF} ${message}" >&2
;;
"ERROR")
echo -e "${RED}[${timestamp}] [ERROR]${COLOR_OFF} ${message}" >&2
;;
"DEBUG")
echo -e "${BLUE}[${timestamp}] [DEBUG]${COLOR_OFF} ${message}"
;;
*)
echo -e "[${timestamp}] ${message}"
;;
esac
}
# Configuration Variables (MODIFY THESE)
IMAGE_NAME="your-image-name"
IMAGE_TAG="v1.0"
EC2_USER="ec2-user"
EC2_HOST="your-ec2-instance-public-dns"
EC2_KEY_PATH="${HOME}/.ssh/your-ec2-key.pem"
DOCKERFILE_PATH="." # Path to directory containing Dockerfile
# Error handling function
error_exit() {
log "ERROR" "$1"
exit 1
}
# Dependency check function (macOS compatible)
check_dependencies() {
log "INFO" "Checking system dependencies..."
# Check Docker
if ! command -v docker &> /dev/null; then
error_exit "Docker is not installed. Please install Docker Desktop for macOS."
fi
log "DEBUG" "Docker is installed: $(docker --version)"
# Check SSH (typically pre-installed on macOS)
if ! command -v ssh &> /dev/null; then
error_exit "SSH is not available. Ensure you have the latest macOS updates."
fi
log "DEBUG" "SSH is available"
# Check SCP
if ! command -v scp &> /dev/null; then
error_exit "SCP is not available. Ensure you have the latest macOS updates."
fi
log "DEBUG" "SCP is available"
}
# Main script execution
main() {
# Ensure script fails if any command fails
set -e
# Check dependencies
check_dependencies
# Validate key file exists and has correct permissions
if [[ ! -f "${EC2_KEY_PATH}" ]]; then
error_exit "SSH key not found at ${EC2_KEY_PATH}"
fi
# Secure key file permissions (macOS and Linux compatible)
chmod 600 "${EC2_KEY_PATH}"
log "INFO" "Secured SSH key permissions"
# Build Docker image
log "INFO" "Building Docker image: ${IMAGE_NAME}:${IMAGE_TAG}"
docker build -t "${IMAGE_NAME}:${IMAGE_TAG}" "${DOCKERFILE_PATH}" || error_exit "Docker build failed"
log "INFO" "Docker image build completed successfully"
# Create temporary directory (macOS compatible)
TEMP_DIR=$(mktemp -d -t docker-transfer-XXXXXXXXXX)
log "DEBUG" "Using temporary directory: ${TEMP_DIR}"
# Save the Docker image to a tar file
log "INFO" "Saving Docker image to tar file"
docker save -o "${TEMP_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar" "${IMAGE_NAME}:${IMAGE_TAG}" || error_exit "Docker save failed"
log "DEBUG" "Image saved: ${TEMP_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar"
# Copy the image to EC2 instance
log "INFO" "Copying Docker image to EC2 instance"
scp -o StrictHostKeyChecking=no \
-i "${EC2_KEY_PATH}" \
"${TEMP_DIR}/${IMAGE_NAME}-${IMAGE_TAG}.tar" \
"${EC2_USER}@${EC2_HOST}:~/" || error_exit "SCP transfer failed"
log "INFO" "Docker image successfully transferred to EC2"
# SSH into EC2 and load the Docker image
log "INFO" "Loading Docker image on EC2 instance"
ssh -o StrictHostKeyChecking=no \
-i "${EC2_KEY_PATH}" "${EC2_USER}@${EC2_HOST}" << EOF
docker load -i ~/"${IMAGE_NAME}-${IMAGE_TAG}.tar"
rm ~/"${IMAGE_NAME}-${IMAGE_TAG}.tar"
docker images | grep "${IMAGE_NAME}"
EOF
log "INFO" "Docker image loaded and verified on EC2"
# Clean up temporary files
rm -rf "${TEMP_DIR}"
log "INFO" "Local temporary files cleaned up"
log "INFO" "🚀 Docker image successfully built and copied to EC2!"
}
# Trap to ensure cleanup happens even if script is interrupted
trap 'log "WARN" "Script interrupted. Performing cleanup..."; rm -rf "${TEMP_DIR}" 2>/dev/null' SIGINT SIGTERM ERR
# Run the main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment