Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Compose | Postgres Container Not Ready When Starting App #146

Open
WilliamFernsV3 opened this issue Dec 15, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@WilliamFernsV3
Copy link

Describe the issue

When I run docker compose up, the app and database containers run in parallel. The downside is that sometimes the database container is not fully ready, resulting in app not being able to connect to the database.

Vapor version

4.106.3

Operating system and version

macOS 15.1.1 24B91 arm64

Swift version

Swift Package Manager - Swift 6.0.2-dev

Steps to reproduce

  1. Create a new Vapor project (vapor new hello -n) (select yes for Fluent, and Postgres for the database)
  2. Run docker compose build and docker compose up
  3. Inspect the logs. There is a big chance that the database is not running before the app is, resulting in the app not being able to connect to the database.

I actually spent the past 4 - 6 hours doing research on how to fix this. A solution is to wait for the database to be ready using healthcheck and then start up the main app.

Here is an improved dockerfile. Not sure, maybe it would be smart to add
condition: service_healthy in other services where it depends on the db service.

# Docker Compose file for Vapor
#
# Install Docker on your system to run and test
# your Vapor app in a production-like environment.
#
# Note: This file is intended for testing and does not
# implement best practices for a production deployment.
#
# Learn more: https://docs.docker.com/compose/reference/
#
#   Build images: docker compose build
#      Start app: docker compose up app
# Start database: docker compose up db
# Run migrations: docker compose run migrate
#       Stop all: docker compose down (add -v to wipe db)
#
version: '3.7'

volumes:
  db_data:

x-shared_environment: &shared_environment
  LOG_LEVEL: ${LOG_LEVEL:-debug}
  DATABASE_HOST: db
  DATABASE_NAME: vapor_database
  DATABASE_USERNAME: vapor_username
  DATABASE_PASSWORD: vapor_password
  
services:
  app:
    image: backend:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    depends_on:
      db:
        condition: service_healthy
    ports:
      - '8080:8080'
    # user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
  migrate:
    image: backend:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    depends_on:
      - db
    command: ["migrate", "--yes"]
    deploy:
      replicas: 0
  revert:
    image: backend:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    depends_on:
      - db
    command: ["migrate", "--revert", "--yes"]
    deploy:
      replicas: 0
  db:
    image: postgres:16-alpine
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U vapor_username -d vapor_database"]
      interval: 5s
      timeout: 10s
      retries: 120
    volumes:
      - db_data:/var/lib/postgresql/data/pgdata
    environment:
      PGDATA: /var/lib/postgresql/data/pgdata
      POSTGRES_USER: vapor_username
      POSTGRES_PASSWORD: vapor_password
      POSTGRES_DB: vapor_database
    ports:
      - '5432:5432'

Outcome

No response

Additional notes

No response

@WilliamFernsV3 WilliamFernsV3 added the bug Something isn't working label Dec 15, 2024
@WilliamFernsV3
Copy link
Author

WilliamFernsV3 commented Dec 15, 2024

Side note, the main app container does not require a command in docker compose, as it is an exact duplicate of the CMD found in the Dockerfile.

If decided to create a patch because of this issue, it would be done to Vapor Toolbox.

@0xTim 0xTim transferred this issue from vapor/vapor Dec 17, 2024
@0xTim
Copy link
Member

0xTim commented Dec 17, 2024

Happy to accept a PR to this repo. We should probably do the same for MySQL and MongoDB as well

@WilliamFernsV3
Copy link
Author

Happy to accept a PR to this repo. We should probably do the same for MySQL and MongoDB as well

Thanks. Will do that soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants