-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-docker.sh
More file actions
executable file
·120 lines (105 loc) · 3.48 KB
/
test-docker.sh
File metadata and controls
executable file
·120 lines (105 loc) · 3.48 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
#!/bin/bash
echo "🐳 Testing Docker Containerization"
echo "=================================="
echo ""
# Step 1: Build PostgreSQL image
echo "📦 Step 1: Building PostgreSQL image..."
docker build -f Dockerfile.postgres -t devices-api-postgres:latest .
if [ $? -ne 0 ]; then
echo "❌ Failed to build PostgreSQL image"
exit 1
fi
echo "✅ PostgreSQL image built successfully"
echo ""
# Step 2: Build Application image
echo "📦 Step 2: Building Application image..."
docker build -t devices-api:latest .
if [ $? -ne 0 ]; then
echo "❌ Failed to build Application image"
exit 1
fi
echo "✅ Application image built successfully"
echo ""
# Step 3: Create network
echo "🌐 Step 3: Creating Docker network..."
docker network create devices-network 2>/dev/null || echo "Network already exists"
echo ""
# Step 4: Start PostgreSQL container
echo "🐘 Step 4: Starting PostgreSQL container..."
docker run -d \
--name devices-postgres \
--network devices-network \
-e POSTGRES_DB=devices_db \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
devices-api-postgres:latest
if [ $? -ne 0 ]; then
echo "⚠️ PostgreSQL container might already be running"
else
echo "✅ PostgreSQL container started"
fi
echo ""
# Step 5: Wait for PostgreSQL to be ready
echo "⏳ Step 5: Waiting for PostgreSQL to be ready..."
sleep 10
echo "✅ PostgreSQL should be ready"
echo ""
# Step 6: Start Application container
echo "🚀 Step 6: Starting Application container..."
docker run -d \
--name devices-api-app \
--network devices-network \
-e SPRING_DATASOURCE_URL=jdbc:postgresql://devices-postgres:5432/devices_db \
-e SPRING_DATASOURCE_USERNAME=postgres \
-e SPRING_DATASOURCE_PASSWORD=postgres \
-p 8080:8080 \
devices-api:latest
if [ $? -ne 0 ]; then
echo "⚠️ Application container might already be running"
else
echo "✅ Application container started"
fi
echo ""
# Step 7: Wait for application to start
echo "⏳ Step 7: Waiting for application to start (30 seconds)..."
sleep 30
echo ""
# Step 8: Check container status
echo "📊 Step 8: Checking container status..."
echo ""
docker ps --filter "name=devices-" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
# Step 9: Check application logs
echo "📋 Step 9: Checking application logs (last 20 lines)..."
echo ""
docker logs --tail 20 devices-api-app
echo ""
# Step 10: Test API endpoint
echo "🧪 Step 10: Testing API endpoint..."
echo ""
echo "Testing GET /api/v1/devices..."
sleep 5
curl -s http://localhost:8080/api/v1/devices | head -20 || echo "⚠️ API might still be starting up"
echo ""
echo ""
# Step 11: Show health check
echo "💚 Step 11: Testing Swagger UI availability..."
curl -s -o /dev/null -w "HTTP Status: %{http_code}\n" http://localhost:8080/swagger-ui.html
echo ""
# Summary
echo "=================================="
echo "🎉 Docker Containerization Test Complete!"
echo ""
echo "📌 Quick Commands:"
echo " - View logs: docker logs -f devices-api-app"
echo " - Stop containers: docker stop devices-api-app devices-postgres"
echo " - Remove containers: docker rm -f devices-api-app devices-postgres"
echo " - Access API: http://localhost:8080/api/v1/devices"
echo " - Access Swagger: http://localhost:8080/swagger-ui.html"
echo ""
echo "✨ Test a complete flow:"
echo ' curl -X POST http://localhost:8080/api/v1/devices \'
echo ' -H "Content-Type: application/json" \'
echo ' -d '"'"'{"name":"Laptop","brand":"Dell"}'"'"
echo ""