-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-build.sh
More file actions
executable file
·109 lines (94 loc) · 2.93 KB
/
docker-build.sh
File metadata and controls
executable file
·109 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# =============================================================================
# Docker build script for michess monorepo applications
# Usage: ./docker-build.sh <app-name> [tag] [--no-cache]
# Example: ./docker-build.sh node-chess
# ./docker-build.sh node-chess my-tag:v1.0
# =============================================================================
set -e
# Check if app name is provided
if [ $# -eq 0 ]; then
echo "❌ Error: App name is required"
echo "Usage: ./docker-build.sh <app-name> [tag] [--no-cache]"
echo ""
echo "Available apps:"
ls -1 apps/ | grep -v ".json" || echo " No apps found"
exit 1
fi
APP_NAME=$1
shift
# Default values
TAG=${1:-"$APP_NAME:latest"}
CACHE_FLAG=""
# Parse remaining arguments
for arg in "$@"; do
case $arg in
--no-cache)
CACHE_FLAG="--no-cache"
shift
;;
esac
done
echo "🐳 Building Docker image for $APP_NAME..."
echo "📦 Tag: $TAG"
echo "🏗️ Context: $(pwd)"
# Ensure we're in the workspace root
if [ ! -f "package.json" ] || [ ! -f "nx.json" ]; then
echo "❌ Error: This script must be run from the workspace root"
exit 1
fi
# Check if app exists
if [ ! -d "apps/$APP_NAME" ]; then
echo "❌ Error: App 'apps/$APP_NAME' does not exist"
echo ""
echo "Available apps:"
ls -1 apps/ | grep -v ".json" || echo " No apps found"
exit 1
fi
# Check if Dockerfile exists
if [ ! -f "apps/$APP_NAME/Dockerfile" ]; then
echo "❌ Error: Dockerfile not found at 'apps/$APP_NAME/Dockerfile'"
exit 1
fi
# Build the Docker image
echo "🔨 Running: docker build $CACHE_FLAG -t $TAG -f apps/$APP_NAME/Dockerfile ."
docker build \
$CACHE_FLAG \
-t "$TAG" \
-f "apps/$APP_NAME/Dockerfile" \
. || {
echo "❌ Docker build failed"
exit 1
}
echo "✅ Docker image built successfully: $TAG"
echo ""
echo "🚀 To run the container:"
# Try to extract PORT from .env files (local takes precedence over .env)
PORT=""
if [ -f "apps/$APP_NAME/.env" ]; then
PORT=$(grep "^PORT=" "apps/$APP_NAME/.env" 2>/dev/null | head -1 | cut -d'=' -f2 | tr -d ' "' | tr -d '\r' || echo "")
fi
if [ -f "apps/$APP_NAME/.env.local" ]; then
LOCAL_PORT=$(grep "^PORT=" "apps/$APP_NAME/.env.local" 2>/dev/null | head -1 | cut -d'=' -f2 | tr -d ' "' | tr -d '\r' || echo "")
if [ -n "$LOCAL_PORT" ]; then
PORT="$LOCAL_PORT"
fi
fi
# Default to 3000 if no port found
if [ -z "$PORT" ]; then
PORT="3000"
fi
# Show appropriate run command
if [ -f "apps/$APP_NAME/.env.local" ]; then
echo " docker run -p $PORT:$PORT --env-file apps/$APP_NAME/.env.local $TAG"
elif [ -f "apps/$APP_NAME/.env" ]; then
echo " docker run -p $PORT:$PORT --env-file apps/$APP_NAME/.env $TAG"
else
echo " docker run -p $PORT:$PORT $TAG"
fi
echo ""
echo "🔍 To inspect the image:"
echo " docker run --rm -it $TAG sh"
echo ""
echo "📊 Image size:"
docker images "$TAG" --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"