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
- Create a new Vapor project (
vapor new hello -n) (select yes for Fluent, and Postgres for the database)
- Run
docker compose build and docker compose up
- 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
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
vapor new hello -n) (select yes for Fluent, and Postgres for the database)docker compose buildanddocker compose upI 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_healthyin other services where it depends on thedbservice.Outcome
No response
Additional notes
No response