You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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_environmentLOG_LEVEL: ${LOG_LEVEL:-debug}DATABASE_HOST: dbDATABASE_NAME: vapor_databaseDATABASE_USERNAME: vapor_usernameDATABASE_PASSWORD: vapor_passwordservices:
app:
image: backend:latestbuild:
context: .environment:
<<: *shared_environmentdepends_on:
db:
condition: service_healthyports:
- '8080:8080'# user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.migrate:
image: backend:latestbuild:
context: .environment:
<<: *shared_environmentdepends_on:
- dbcommand: ["migrate", "--yes"]deploy:
replicas: 0revert:
image: backend:latestbuild:
context: .environment:
<<: *shared_environmentdepends_on:
- dbcommand: ["migrate", "--revert", "--yes"]deploy:
replicas: 0db:
image: postgres:16-alpinehealthcheck:
test: ["CMD-SHELL", "pg_isready -U vapor_username -d vapor_database"]interval: 5stimeout: 10sretries: 120volumes:
- db_data:/var/lib/postgresql/data/pgdataenvironment:
PGDATA: /var/lib/postgresql/data/pgdataPOSTGRES_USER: vapor_usernamePOSTGRES_PASSWORD: vapor_passwordPOSTGRES_DB: vapor_databaseports:
- '5432:5432'
Outcome
No response
Additional notes
No response
The text was updated successfully, but these errors were encountered:
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 build
anddocker compose up
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 thedb
service.Outcome
No response
Additional notes
No response
The text was updated successfully, but these errors were encountered: