Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN apt-get update && apt-get install -y \

# Copy requirements and install
COPY api/requirements.txt .
RUN pip install -r requirements.txt
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org -r requirements.txt

# Copy application code from api folder
COPY api/ .
Expand Down
17 changes: 14 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: "0.1"

services:
postgresql:
image: postgres:15
Expand All @@ -13,6 +15,8 @@ services:
interval: 5s
timeout: 5s
retries: 5
networks:
- tzu_net

backend:
build:
Expand All @@ -21,23 +25,24 @@ services:
command: ["./backend.entrypoint.sh"]
# volumes: # Uncomment for development
# - ../api:/app
ports:
- "8000:8000"
depends_on:
postgresql:
condition: service_healthy
environment:
- DATABASE_URL=postgresql://postgres:postgres@postgresql:5432/tzu_db
env_file:
- ../.env

networks:
- tzu_net

frontend:
build:
context: ../frontend
dockerfile: ../docker/Dockerfile.frontend
volumes:
- frontend_build:/app
networks:
- tzu_net

nginx:
image: nginx:alpine
Expand All @@ -49,7 +54,13 @@ services:
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- frontend_build:/usr/share/nginx/html:ro
networks:
- tzu_net

volumes:
postgres_data:
frontend_build:

networks:
tzu_net:
driver: bridge
82 changes: 80 additions & 2 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,87 @@ cd docker
$COMPOSE_CMD up -d --build
cd ..

# Wait briefly for services to come up
# Function to check container status
check_container_status() {
local compose_cmd="$1"
local max_attempts=30
local attempt=1

echo "⏳ Waiting for containers to start..."

while [ $attempt -le $max_attempts ]; do
cd docker

# Get container status
local containers_info=$($compose_cmd ps --format "table {{.Service}}\t{{.Status}}" 2>/dev/null)
local all_running=true
local failed_services=""

# Check each expected service
for service in postgresql backend frontend nginx; do
local service_status=$(echo "$containers_info" | grep "^$service" | awk '{print $2}')

if [[ -z "$service_status" ]]; then
all_running=false
failed_services="$failed_services $service(not_found)"
elif [[ "$service_status" != "running" ]] && [[ "$service_status" != "Up" ]] && [[ ! "$service_status" =~ ^Up ]]; then
all_running=false
failed_services="$failed_services $service($service_status)"
fi
done

cd ..

if [ "$all_running" = true ]; then
echo "βœ… All containers are running"
return 0
else
if [ $attempt -eq $max_attempts ]; then
echo "❌ Container validation failed after $max_attempts attempts"
echo "❌ Failed services:$failed_services"
return 1
fi
echo "⏳ Attempt $attempt/$max_attempts: Waiting for containers to be ready..."
sleep 5
attempt=$((attempt + 1))
fi
done
}

# Function to check health status for services with healthchecks
check_health_status() {
local compose_cmd="$1"

echo "πŸ” Checking service health..."

cd docker

# Check postgresql health (it has a healthcheck)
local pg_health=$($compose_cmd ps postgresql --format "table {{.Status}}" 2>/dev/null | tail -n +2)

cd ..

if [[ "$pg_health" =~ "healthy" ]]; then
echo "βœ… PostgreSQL is healthy"
return 0
else
echo "⚠️ PostgreSQL health check: $pg_health"
# Don't fail here, just warn - container might still be starting
return 0
fi
}

# Wait for containers to start and validate their status
echo "⏳ Waiting for system initialization..."
sleep 10
if ! check_container_status "$COMPOSE_CMD"; then
echo "❌ Failed to start all containers properly"
echo "πŸ’‘ Try running: cd docker && $COMPOSE_CMD logs"
echo "πŸ’‘ Or check individual container status: cd docker && $COMPOSE_CMD ps"
exit 1
fi

# Check health status
check_health_status "$COMPOSE_CMD"

# Get backend logs to capture credentials
echo "πŸ”§ Capturing initialization credentials..."
Expand Down