Skip to content

Commit 2556b39

Browse files
committed
optimized image
1 parent 56e3d95 commit 2556b39

3 files changed

Lines changed: 42 additions & 48 deletions

File tree

Dockerfile

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,60 @@
1-
FROM node:20-alpine AS base
2-
3-
RUN apk add --no-cache curl bash ca-certificates
4-
5-
# Install Bun
6-
RUN curl -fsSL https://bun.sh/install | bash
7-
ENV PATH="/root/.bun/bin:$PATH"
8-
9-
RUN if [ -f /root/.bun/bin/bun ]; then \
10-
/root/.bun/bin/bun --version && \
11-
if [ ! -f /root/.bun/bin/bunx ]; then \
12-
ln -s /root/.bun/bin/bun /root/.bun/bin/bunx; \
13-
fi; \
14-
else \
15-
echo "ERROR: Bun not installed properly" && exit 1; \
16-
fi
17-
1+
FROM oven/bun:1.3 AS base
182
WORKDIR /app
193

20-
214
# -----------------------------
225
# deps stage - cache dependencies
236
# -----------------------------
24-
25-
267
FROM base AS deps
278

289
COPY package.json bun.lock* ./
2910
COPY prisma ./prisma
30-
3111
RUN bun install --frozen-lockfile
3212
RUN bunx prisma generate
3313

34-
3514
# -----------------------------
36-
# development stage
15+
# build stage - compile TypeScript to JavaScript
3716
# -----------------------------
17+
FROM deps AS build
3818

19+
COPY . .
20+
RUN bun build src/server.ts --outdir dist
3921

40-
FROM base AS development
22+
# -----------------------------
23+
# prod deps stage - install only production dependencies
24+
# -----------------------------
25+
FROM base AS prod-deps
4126

42-
COPY --from=deps /app/node_modules ./node_modules
43-
COPY --from=deps /app/src/generated ./src/generated
27+
COPY package.json bun.lock* ./
28+
RUN bun install --production --frozen-lockfile
4429

30+
# -----------------------------
31+
# development stage
32+
# -----------------------------
33+
FROM deps AS development
4534
COPY . .
46-
4735
EXPOSE 3000
48-
4936
CMD ["bun", "src/server.ts"]
5037

51-
5238
# -----------------------------
5339
# production stage
5440
# -----------------------------
5541

5642

57-
FROM base AS production
43+
FROM prod-deps AS production
5844

5945
RUN addgroup -g 1001 -S nodejs && adduser -S bunjs -u 1001
60-
RUN cp -r /root/.bun /usr/local/bun && chown -R bunjs:nodejs /usr/local/bun
6146

62-
COPY --from=deps --chown=bunjs:nodejs /app/node_modules ./node_modules
6347
COPY --from=deps --chown=bunjs:nodejs /app/src/generated ./src/generated
64-
65-
COPY --chown=bunjs:nodejs src ./src
66-
COPY --chown=bunjs:nodejs package.json ./
67-
COPY --chown=bunjs:nodejs prisma ./prisma
48+
COPY --from=build --chown=bunjs:nodejs /app/dist ./dist
6849

6950
USER bunjs
7051

7152
ENV NODE_ENV=production
7253
ENV PORT=3000
73-
ENV PATH="/usr/local/bun/bin:$PATH"
7454

7555
EXPOSE 3000
7656

7757
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
78-
CMD curl -sf http://localhost:3000/health || exit 1
58+
CMD wget -qO- http://localhost:3000/health || exit 1
7959

80-
CMD ["sh", "-c", "bun src/server.ts"]
60+
CMD ["bun", "dist/server.js"]

LOCAL_DEVELOPMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bun run local
3232
#### What the script does:
3333
1. **Starts Postgres**: Launches the `db` container.
3434
2. **Installs Extensions**: Pre-installs `pgcrypto`, `uuid-ossp`, and `pg_stat_statements` into the `public` schema.
35-
3. **Loads Local Seed**: If `public` has no tables, the script loads `seed/dump.sql` into the DB. This is the default and recommended local flow.
35+
3. **Loads Local Seed**: The script will attempt to load `seed/dump.sql` into the DB only when there are no existing user tables present in the database. If the database already contains tables (non-system tables), the seed step will be skipped to avoid accidentally overwriting existing data. This is the default and recommended local flow.
3636
4. **Starts the API**: Launches the `api` container and waits for it to be healthy.
3737

3838
Flags:

scripts/setup-local.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
!/usr/bin/env bash
12
# =============================================================================
23
# setup-local.sh
34
#
@@ -15,7 +16,6 @@
1516
# --skip-seed Skip seeding entirely (just start containers)
1617
# =============================================================================
1718

18-
!/usr/bin/env bash
1919
set -euo pipefail
2020

2121
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -57,7 +57,7 @@ fi
5757

5858
info "Loading environment from $ENV_FILE"
5959
set -o allexport
60-
# shellcheck disable=SC1090
60+
# shellcheck disable: SC1090
6161
source <(grep -E '^[A-Za-z_][A-Za-z0-9_]*=' "$ENV_FILE" | sed 's/\r//')
6262
set +o allexport
6363

@@ -77,7 +77,7 @@ docker compose up db --build -d
7777
# 2. Wait for postgres to be healthy
7878
# ========================================================
7979
info "Waiting for postgres to be healthy..."
80-
RETRIES=20
80+
RETRIES=10
8181
until docker compose exec -T db pg_isready -U postgres -d coc -h 127.0.0.1 -p 5432 -q 2>/dev/null; do
8282
RETRIES=$((RETRIES - 1))
8383
if [[ $RETRIES -le 0 ]]; then
@@ -92,7 +92,7 @@ info "Postgres is healthy."
9292
# 3. Install required extensions
9393
# ========================================================
9494
info "Installing required extensions into postgres..."
95-
docker compose exec -T db psql -U postgres -d coc <<'EXTSQL'
95+
docker compose exec -T db psql -v ON_ERROR_STOP=1 -U postgres -d coc <<'EXTSQL'
9696
CREATE SCHEMA IF NOT EXISTS extensions;
9797
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
9898
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
@@ -103,6 +103,8 @@ info "Extensions installed."
103103

104104
# ========================================================
105105
# 4. Check if seeding is required
106+
# - If user passed --skip-seed, we skip
107+
# - Otherwise, if the DB already contains user tables (non-system), skip seeding
106108
# ========================================================
107109
if [[ "$SKIP_SEED" == true ]]; then
108110
warn "--skip-seed passed. Skipping dump and load."
@@ -112,9 +114,21 @@ else
112114
error "$DUMP_FILE not found. Create the seed SQL at $DUMP_FILE and re-run this script."
113115
fi
114116

115-
info "Loading local seed SQL from $DUMP_FILE ..."
116-
docker compose exec -T db psql -U postgres -d coc < "$DUMP_FILE"
117-
info "Seed complete."
117+
info "Checking whether database already has user tables..."
118+
TABLE_COUNT=$(docker compose exec -T db psql -U postgres -d coc -t -A -c "SELECT count(*) FROM pg_catalog.pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema');" | tr -d '[:space:]' || true)
119+
120+
if [[ -z "$TABLE_COUNT" ]]; then
121+
warn "Could not determine table count; proceeding with seed."
122+
info "Loading local seed SQL from $DUMP_FILE ..."
123+
docker compose exec -T db psql -v ON_ERROR_STOP=1 -U postgres -d coc < "$DUMP_FILE"
124+
info "Seed complete."
125+
elif [[ "$TABLE_COUNT" -gt 0 ]]; then
126+
warn "Database already has ${TABLE_COUNT} user tables; skipping seed."
127+
else
128+
info "No existing user tables found. Loading local seed SQL from $DUMP_FILE ..."
129+
docker compose exec -T db psql -v ON_ERROR_STOP=1 -U postgres -d coc < "$DUMP_FILE"
130+
info "Seed complete."
131+
fi
118132
fi
119133

120134

@@ -126,7 +140,7 @@ DATABASE_URL="$LOCAL_DATABASE_URL" \
126140
docker compose up api --build -d
127141

128142
info "Waiting for api to be healthy..."
129-
RETRIES=15
143+
RETRIES=10
130144
until curl -sf http://localhost:3000/health >/dev/null 2>&1; do
131145
RETRIES=$((RETRIES - 1))
132146
if [[ $RETRIES -le 0 ]]; then

0 commit comments

Comments
 (0)