Skip to content

Conversation

2u841r
Copy link

@2u841r 2u841r commented Aug 19, 2025

Why:
Simplifies setup and deployment.
Ensures consistent environment across machines.

tested multiple times on EC2.

Copy link

vercel bot commented Aug 19, 2025

@2u841r is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Comment on lines +16 to +18
depends_on:
- db
- redis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app container will fail to build because database migrations run during the build process, but the PostgreSQL database is not available during build time.

View Details
📝 Patch Details
diff --git a/docker-compose.yml b/docker-compose.yml
index c1517c2..d9a8c0e 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -14,8 +14,10 @@ services:
     ports:
       - "3000:3000"
     depends_on:
-      - db
-      - redis
+      db:
+        condition: service_healthy
+      redis:
+        condition: service_started
 
   db:
     image: postgres:16-alpine
@@ -24,6 +26,13 @@ services:
       POSTGRES_USER: postgres
       POSTGRES_PASSWORD: postgres
       POSTGRES_DB: ai_chatbot
+      PGUSER: postgres
+    healthcheck:
+      test: ["CMD-SHELL", "pg_isready -d ai_chatbot -U postgres"]
+      interval: 10s
+      timeout: 5s
+      retries: 5
+      start_period: 10s
     ports:
       - "5432:5432"
     volumes:

Analysis

The current Docker setup has a critical timing issue. The package.json build script runs database migrations ("build": "tsx lib/db/migrate && next build"), which happens during the Docker image build process in the Dockerfile. However, the PostgreSQL database container (db) is only available at runtime when services are started with docker-compose up, not during the build phase.

When docker-compose up runs, it will:

  1. Build the app container, which executes RUN pnpm install and then later CMD ["pnpm", "dev"]
  2. During development, pnpm dev might trigger builds that call the migrate script
  3. The migrate script tries to connect to postgres://postgres:postgres@db:5432/ai_chatbot
  4. At this point, the db service may not be ready, causing connection failures

Even with depends_on, Docker Compose only waits for the container to start, not for PostgreSQL to be ready to accept connections. The migration files show this is a real database with multiple tables and schema changes that must succeed for the application to work.


Recommendation

Add a health check for the PostgreSQL service and modify the app service to wait for the database to be ready before starting. Here's the recommended fix:

  1. Add health check to the database service:
db:
  image: postgres:16-alpine
  container_name: ai-chatbot-db
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    POSTGRES_DB: ai_chatbot
  healthcheck:
    test: ["CMD-READY", "pg_isready", "-U", "postgres", "-d", "ai_chatbot"]
    interval: 10s
    timeout: 5s
    retries: 5
  ports:
    - "5432:5432"
  volumes:
    - pgdata:/var/lib/postgresql/data
  1. Update the app service dependencies:
app:
  build: .
  container_name: ai-chatbot
  depends_on:
    db:
      condition: service_healthy
    redis:
      condition: service_started
  # ... rest of configuration
  1. Consider running migrations at container startup instead of during dev server startup by modifying the Dockerfile CMD or adding an entrypoint script that runs migrations before starting the dev server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant