-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-dev.sh
More file actions
executable file
·393 lines (346 loc) · 10.6 KB
/
docker-dev.sh
File metadata and controls
executable file
·393 lines (346 loc) · 10.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
# ==============================================================================
# Postgresus - Build & Run Script for Docker Desktop
# ==============================================================================
# This script helps you build and run Postgresus locally on your Ubuntu machine
# using Docker Desktop.
#
# Usage:
# ./docker-dev.sh build - Build the Docker image
# ./docker-dev.sh run - Run the container (build first if needed)
# ./docker-dev.sh start - Start with optional test databases
# ./docker-dev.sh stop - Stop all containers
# ./docker-dev.sh logs - View logs
# ./docker-dev.sh shell - Open shell in the container
# ./docker-dev.sh clean - Remove containers, volumes, and images
# ./docker-dev.sh help - Show this help message
# ==============================================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Project configuration
PROJECT_NAME="postgresus"
IMAGE_NAME="postgresus:dev"
CONTAINER_NAME="postgresus"
# Helper functions
print_banner() {
echo -e "${BLUE}"
echo "╔══════════════════════════════════════════════════════════════════╗"
echo "║ 🐘 POSTGRESUS DEV TOOLS 🐘 ║"
echo "╚══════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
# Check if Docker is running
check_docker() {
if ! docker info >/dev/null 2>&1; then
error "Docker is not running. Please start Docker Desktop and try again."
fi
}
# Build the Docker image
build() {
print_banner
check_docker
info "Building Postgresus Docker image..."
docker build \
--tag "$IMAGE_NAME" \
--build-arg APP_VERSION=dev-$(date +%Y%m%d-%H%M%S) \
--progress=plain \
.
success "Docker image built successfully: $IMAGE_NAME"
}
# Run a single container
run() {
print_banner
check_docker
# Check if image exists, build if not
if ! docker image inspect "$IMAGE_NAME" >/dev/null 2>&1; then
warn "Image not found. Building first..."
build
fi
# Stop existing container if running
if docker ps -q -f "name=$CONTAINER_NAME" | grep -q .; then
info "Stopping existing container..."
docker stop "$CONTAINER_NAME" >/dev/null 2>&1 || true
docker rm "$CONTAINER_NAME" >/dev/null 2>&1 || true
fi
info "Starting Postgresus container..."
docker run -d \
--name "$CONTAINER_NAME" \
-p 443:443 \
-p 4005:4005 \
-v postgresus-data:/postgresus-data \
--restart unless-stopped \
"$IMAGE_NAME"
success "Postgresus is running with HTTPS!"
echo ""
echo -e "🔒 HTTPS: ${GREEN}https://localhost${NC} (self-signed certificate)"
echo -e "🔓 HTTP: ${GREEN}http://localhost:4005${NC} (redirects to HTTPS)"
echo ""
echo -e "${YELLOW}Note: Your browser may show a security warning for the self-signed certificate.${NC}"
echo -e "${YELLOW}Click 'Advanced' -> 'Proceed to localhost' to continue.${NC}"
echo ""
}
# Start with docker compose (includes optional test databases)
start() {
print_banner
check_docker
info "Setting up Docker Compose configuration..."
# Create docker-compose.yml from template if not exists
if [ ! -f "docker-compose.yml" ]; then
create_compose_file
fi
# Check for test databases flag
if [ "$1" == "--with-tests" ] || [ "$1" == "-t" ]; then
info "Starting Postgresus with test databases..."
docker compose --profile testing up -d --build
else
info "Starting Postgresus..."
docker compose up -d --build
fi
success "Postgresus is running with HTTPS!"
echo ""
echo -e "🔒 HTTPS: ${GREEN}https://localhost${NC} (self-signed certificate)"
echo -e "🔓 HTTP: ${GREEN}http://localhost:4005${NC} (redirects to HTTPS)"
echo ""
}
# Stop containers
stop() {
print_banner
check_docker
info "Stopping Postgresus..."
if [ -f "docker-compose.yml" ]; then
docker compose --profile testing down
else
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
fi
success "Postgresus stopped."
}
# View logs
logs() {
check_docker
if [ -f "docker-compose.yml" ]; then
docker compose logs -f postgresus
else
docker logs -f "$CONTAINER_NAME"
fi
}
# Open shell in container
shell() {
check_docker
info "Opening shell in Postgresus container..."
docker exec -it "$CONTAINER_NAME" /bin/bash
}
# Clean everything
clean() {
print_banner
check_docker
warn "This will remove ALL Postgresus containers, volumes, and images!"
read -p "Are you sure? (y/N): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
info "Aborted."
exit 0
fi
info "Stopping and removing containers..."
if [ -f "docker-compose.yml" ]; then
docker compose --profile testing down -v --rmi local
fi
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
info "Removing volumes..."
docker volume rm postgresus-data 2>/dev/null || true
docker volume rm test-postgres-data 2>/dev/null || true
docker volume rm test-mysql-data 2>/dev/null || true
docker volume rm test-mongodb-data 2>/dev/null || true
info "Removing images..."
docker rmi "$IMAGE_NAME" 2>/dev/null || true
success "Cleanup complete."
}
# Create docker-compose.yml
create_compose_file() {
cat > docker-compose.yml << 'EOF'
version: "3.8"
# Docker Compose for local development and testing with Docker Desktop
# Run: docker compose up -d --build
services:
# Main Postgresus application (build from source)
postgresus:
build:
context: .
dockerfile: Dockerfile
args:
- APP_VERSION=dev
image: postgresus:dev
container_name: postgresus
ports:
- "443:443"
- "4005:4005"
volumes:
- postgresus-data:/postgresus-data
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-kf", "https://localhost/api/v1/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
networks:
- postgresus-network
# Optional: Test PostgreSQL database for backup testing
test-postgres:
image: postgres:17
container_name: test-postgres
environment:
- POSTGRES_DB=testdb
- POSTGRES_USER=testuser
- POSTGRES_PASSWORD=testpassword
ports:
- "5432:5432"
volumes:
- test-postgres-data:/var/lib/postgresql/data
networks:
- postgresus-network
profiles:
- testing
# Optional: Test MySQL database for backup testing
test-mysql:
image: mysql:8.0
container_name: test-mysql
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=testdb
- MYSQL_USER=testuser
- MYSQL_PASSWORD=testpassword
ports:
- "3306:3306"
volumes:
- test-mysql-data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
networks:
- postgresus-network
profiles:
- testing
# Optional: Test MongoDB for backup testing
test-mongodb:
image: mongo:7.0
container_name: test-mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootpassword
- MONGO_INITDB_DATABASE=testdb
ports:
- "27017:27017"
volumes:
- test-mongodb-data:/data/db
command: mongod --auth
networks:
- postgresus-network
profiles:
- testing
volumes:
postgresus-data:
name: postgresus-data
test-postgres-data:
name: test-postgres-data
test-mysql-data:
name: test-mysql-data
test-mongodb-data:
name: test-mongodb-data
networks:
postgresus-network:
name: postgresus-network
driver: bridge
EOF
success "Created docker-compose.yml"
}
# Show status
status() {
print_banner
check_docker
echo "Container status:"
docker ps -a --filter "name=postgresus" --filter "name=test-" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
echo "Volumes:"
docker volume ls --filter "name=postgresus" --filter "name=test-"
echo ""
echo "Images:"
docker images --filter "reference=postgresus*"
}
# Show help
show_help() {
print_banner
echo "Commands:"
echo " build Build the Docker image from source"
echo " run Run a single container (simple mode)"
echo " start Start with Docker Compose"
echo " start -t Start with test databases (PostgreSQL, MySQL, MongoDB)"
echo " stop Stop all containers"
echo " logs View container logs"
echo " shell Open bash shell in the container"
echo " status Show container and volume status"
echo " clean Remove containers, volumes, and images"
echo " help Show this help message"
echo ""
echo "Quick start:"
echo " 1. ./docker-dev.sh build # Build the image"
echo " 2. ./docker-dev.sh run # Start the container"
echo " 3. Open https://localhost (accept the self-signed certificate)"
echo ""
echo "With test databases for backup testing:"
echo " ./docker-dev.sh start -t"
echo ""
echo "Test database connection details:"
echo " PostgreSQL: localhost:5432, user=testuser, password=testpassword, db=testdb"
echo " MySQL: localhost:3306, user=testuser, password=testpassword, db=testdb"
echo " MongoDB: localhost:27017, user=root, password=rootpassword"
}
# Main
case "${1:-help}" in
build)
build
;;
run)
run
;;
start)
start "$2"
;;
stop)
stop
;;
logs)
logs
;;
shell)
shell
;;
status)
status
;;
clean)
clean
;;
help|--help|-h)
show_help
;;
*)
error "Unknown command: $1. Use './docker-dev.sh help' for usage."
;;
esac