Skip to content

Almost working devcontainers #71

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node-postgres
{
"name": "Bun & Postgres",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"dockerComposeFile": [
"docker/docker-compose.yml",
"docker/docker-compose.workspace.yml"
],
"service": "workspace",
"workspaceFolder": "/app",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {},
Expand All @@ -16,14 +19,6 @@
"ghcr.io/itsmechlark/features/postgresql:1.3.3": {}
},
"waitFor": "onCreateCommand",
"updateContentCommand": "bun i",
"postAttachCommand": {
"app": "bun dev",
"studio": "bun db:studio",
"docs": "bun docs"
},
// "postCreateCommand": "bun db:migrate && bun db:seed",
"postCreateCommand": "./scripts/create-db.sh",
"customizations": {
"vscode": {
"extensions": [
Expand Down
Binary file modified bun.lockb
Binary file not shown.
49 changes: 0 additions & 49 deletions docker-compose.yml

This file was deleted.

10 changes: 10 additions & 0 deletions docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This is needed for devcontainers. We can't specify the .env files in the devcontainer.json file
# so we need to rely on docker compose to pick up this file by default...
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=medium
POSTGRES_HOST=0.0.0.0
POSTGRES_PORT=5432
JWT_SECRET=supersecretkey
JWT_ALGORITHM=HS256
APP_PORT=3000
13 changes: 13 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM oven/bun:1.0.6-slim

WORKDIR /app

RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git

COPY package.json /app/package.json
COPY bun.lockb /app/bun.lockb

RUN bun install

13 changes: 13 additions & 0 deletions docker/docker-compose.workspace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.8'

services:
workspace:
build:
context: ..
dockerfile: docker/Dockerfile
restart: unless-stopped
command: ["sleep", "infinity"]
volumes:
- ..:/app


57 changes: 57 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.8'

x-common:
&commmon
build:
context: ..
dockerfile: docker/Dockerfile
depends_on:
db:
condition: service_healthy
env_file:
- ../environments/local/.env.db
- ../environments/local/.env.app
environment:
- POSTGRES_HOST=db
- POSTGRES_PORT=${POSTGRES_PORT}
volumes:
- ..:/app


services:
db:
image: postgres:16
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_PORT=${POSTGRES_PORT}
healthcheck:
test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}"]
interval: 5s
timeout: 30s
retries: 10
ports:
- ${POSTGRES_PORT}:${POSTGRES_PORT}

db-init:
<<: *commmon
command: ["bun", "db:migrate", "&&", "bun", "db:seed"]
restart: no

app:
<<: *commmon
restart: unless-stopped
command: ["bun", "dev"]
depends_on:
db-init:
condition: service_completed_successfully
ports:
- ${APP_PORT}:${APP_PORT}

volumes:
postgres-data:

3 changes: 3 additions & 0 deletions environments/local/.env.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
JWT_SECRET=supersecretkey
JWT_ALGORITHM=HS256
APP_PORT=3000
4 changes: 1 addition & 3 deletions .env → environments/local/.env.db
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ POSTGRES_PASSWORD=postgres
POSTGRES_DB=medium
POSTGRES_HOST=0.0.0.0
POSTGRES_PORT=5432
JWT_SECRET=supersecretkey
JWT_ALGORITHM=HS256
APP_PORT=3000

15 changes: 0 additions & 15 deletions scripts/create-db.sh

This file was deleted.

22 changes: 12 additions & 10 deletions scripts/create-start-container-with-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
# 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"
current_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$current_path/.."


env_prefix="environments"
# Default env folder
env_folder="$env_prefix/local"

# Determine which env file to use based on NODE_ENV
if [ "$NODE_ENV" == "production" ]; then
env_file=".env.production"
env_folder="$env_prefix/production"
elif [ "$NODE_ENV" == "development" ]; then
env_file=".env.development"
env_folder="$env_prefix/development"
elif [ "$NODE_ENV" == "test" ]; then
env_file=".env.test"
env_folder="$env_prefix/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 "$@"
docker-compose --env-file "$env_folder/.env.db" --env-file "$env_folder/.env.app" -f docker/docker-compose.yml up "$@"
Loading