Skip to content

Commit

Permalink
feat: support bun-like .env hierarchy in docker compose (#40)
Browse files Browse the repository at this point in the history
* feat: support bun-like .env hierarchy in docker compose

* feat: pass along arguments

this allows the usage of `bun db:up -d`
  • Loading branch information
yamcodes committed Sep 26, 2023
1 parent 3a46141 commit 2bf8fb0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions docker-compose.yaml → docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
image: postgres
restart: always
ports:
- 5432:5432
- ${POSTGRES_PORT}:5432
environment:
PGUSER: ${POSTGRES_USER}
POSTGRES_DB: ${POSTGRES_DB}
Expand All @@ -19,4 +19,4 @@ services:
env_file:
- .env
volumes:
pgdata:
pgdata:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"db:up": "docker-compose up -d",
"db:up": "./scripts/create-start-container-with-env.sh",
"db:generate": "bun drizzle-kit generate:pg --config=db/config.ts",
"db:migrate": "bun run db/migrations/migrate.ts",
"db:push": "bun drizzle-kit push:pg --config=db/config.ts",
Expand Down
24 changes: 24 additions & 0 deletions scripts/create-start-container-with-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# this script extends docker-compose up by supporting .env files in the same way as bun
# see: https://bun.sh/docs/runtime/env#setting-environment-variables

# Default env file
env_file=".env"

# Determine which env file to use based on NODE_ENV
if [ "$NODE_ENV" == "production" ]; then
env_file=".env.production"
elif [ "$NODE_ENV" == "development" ]; then
env_file=".env.development"
elif [ "$NODE_ENV" == "test" ]; then
env_file=".env.test"
fi

# If .env.local exists, use that instead
if [ -f ".env.local" ]; then
env_file=".env.local"
fi

# Run docker-compose with the selected env file and pass along any arguments
docker-compose --env-file $env_file up "$@"

0 comments on commit 2bf8fb0

Please sign in to comment.