Skip to content

Commit 3238073

Browse files
authored
feat: add Docker & Docker Compose For Dev Mode. (#31)
1 parent 0ccae72 commit 3238073

File tree

9 files changed

+239
-43
lines changed

9 files changed

+239
-43
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
.env.example
3+
dist/
4+
ignore/
5+
node_modules/*

.env.docker

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For Docker
2+
# Paste all this in your .env file and run it with docker-compose up -d
3+
4+
DATABASE_URL="postgresql://postgres:postgres@postgres:5432/postgres"
5+
JWT_SECRET="secretjwt"
6+
API_PORT=3000
7+
POSTGRES_PORT=5432
8+
POSTGRES_PATH=postgres-data
9+
POSTGRES_HOST=postgres
10+
POSTGRES_USERNAME=postgres
11+
POSTGRES_PASSWORD=postgres
12+
STRIPE_SK_KEY=<YOUR STRIPE SK KEY>

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ dist/
66
ignore/
77
prisma/data
88
.env
9-
.env.*
109
! .env.example
1110
.vscode
1211
node_modules

Dockerfile

Lines changed: 0 additions & 17 deletions
This file was deleted.

Dockerfile.dev

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Stage 1: Installer Stage
2+
FROM node:22-alpine3.20 AS installer
3+
4+
WORKDIR /usr/src/app
5+
6+
RUN corepack enable && corepack prepare [email protected] --activate
7+
COPY package.json pnpm-lock.yaml ./
8+
COPY prisma ./prisma
9+
10+
RUN apk add --no-cache make gcc g++ python3 && \
11+
if [ -f pnpm-lock.yaml ]; then \
12+
pnpm install --frozen-lockfile --ignore-scripts; \
13+
else \
14+
echo "pnpm-lock.yaml not found" && exit 1; \
15+
fi && \
16+
npm rebuild bcrypt --build-from-source && \
17+
apk del make gcc g++ python3
18+
19+
COPY . .
20+
21+
# Stage 2: Development Stage
22+
FROM node:22-alpine3.20 AS development
23+
24+
WORKDIR /usr/src/app
25+
RUN corepack enable && corepack prepare [email protected] --activate
26+
COPY --from=installer /usr/src/app/ ./
27+
CMD ["pnpm", "run", "dev:docker"]

Dockerfile.prod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Stage 1: Builder Stage
2+
FROM node:22-alpine3.20 AS builder
3+
WORKDIR /usr/src/app
4+
5+
RUN corepack enable && corepack prepare [email protected] --activate
6+
7+
COPY package.json pnpm-lock.yaml ./
8+
COPY prisma ./prisma
9+
10+
RUN apk add --no-cache make gcc g++ python3 && \
11+
if [ -f pnpm-lock.yaml ]; then \
12+
pnpm install --frozen-lockfile --ignore-scripts; \
13+
else \
14+
echo "pnpm-lock.yaml not found" && exit 1; \
15+
fi && \
16+
npm rebuild bcrypt --build-from-source && \
17+
apk del make gcc g++ python3
18+
19+
COPY . .
20+
21+
RUN DATABASE_URL=$DATABASE_URL pnpm run build
22+
23+
# Stage 2: Production Stage
24+
FROM node:22-alpine3.20 AS production
25+
WORKDIR /usr/src/app
26+
27+
COPY --from=builder /usr/src/app/dist ./dist
28+
COPY --from=builder /usr/src/app/node_modules/ ./node_modules
29+
30+
EXPOSE 3000
31+
32+
CMD [ "node", "dist/index.js" ]

docker-compose.yml

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
# docker-compose.yml
2-
3-
# THIS IS BROKEN RN
4-
5-
version: "3.9"
61
services:
7-
postgres:
8-
image: postgres:latest
9-
environment:
10-
POSTGRES_USER: jasper
11-
POSTGRES_PASSWORD: jasper
12-
ports:
13-
- '5432:5432'
14-
volumes:
15-
- db:/var/lib/postgresql/data
16-
172
api:
3+
container_name: api-docker
184
build:
195
context: .
20-
environment:
21-
DB_SCHEMA: postgres
22-
DB_USER: postgres
23-
DB_PASSWORD: postgres
24-
DB_HOST: postgres
6+
dockerfile: Dockerfile.dev
7+
env_file:
8+
- .env
9+
ports:
10+
- ${API_PORT}:3000
2511
depends_on:
26-
- postgres
12+
postgres:
13+
condition: service_healthy
14+
volumes:
15+
- ./:/usr/src/app
16+
- /usr/src/app/node_modules
17+
18+
postgres:
19+
container_name: prisma-postgres
20+
image: postgres:alpine
21+
restart: always
22+
env_file:
23+
- .env
2724
ports:
28-
- '3000:3000'
25+
- ${POSTGRES_PORT}:5432
26+
volumes:
27+
- ${POSTGRES_PATH}:/var/lib/postgresql/data
28+
healthcheck:
29+
test: ["CMD-SHELL", "pg_isready -U postgres"]
30+
interval: 10s
31+
timeout: 5s
32+
retries: 5
2933

3034
volumes:
31-
db:
35+
postgres-data:
36+
external: false

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"packageManager": "[email protected]",
1414
"scripts": {
15+
"preinstall": "npx only-allow pnpm",
1516
"start": "node dist/index.js",
1617
"postinstall": "npx prisma generate && tsc",
1718
"watch-node": "nodemon dist/index.js",
@@ -22,7 +23,9 @@
2223
"heroku-prebuild": "echo This runs before Heroku installs dependencies.",
2324
"heroku-postbuild": "echo This runs after Heroku installs dependencies, but before Heroku prunes and caches dependencies.",
2425
"heroku-cleanup": "echo This runs after Heroku prunes and caches dependencies.",
25-
"prepare": "husky"
26+
"prepare": "husky",
27+
"prisma:docker": "npx prisma generate && npx prisma migrate dev",
28+
"dev:docker": "pnpm prisma:docker && pnpm dev"
2629
},
2730
"keywords": [],
2831
"author": "Jasper Mayone <[email protected]>",
@@ -42,7 +45,8 @@
4245
"puppeteer": "^22.15.0",
4346
"request-ip": "^3.3.0",
4447
"response-time": "^2.3.2",
45-
"stripe": "^16.8.0"
48+
"stripe": "^16.8.0",
49+
"ts-node": "^10.9.2"
4650
},
4751
"devDependencies": {
4852
"@types/bcrypt": "^5.0.2",

0 commit comments

Comments
 (0)