Skip to content

Commit e8814a0

Browse files
committed
ci: 📦 Docker Support
Add support for docker using docker file and docker compose which spins up the database and the site in their respective containers
1 parent 3b0b1c5 commit e8814a0

File tree

9 files changed

+145
-16
lines changed

9 files changed

+145
-16
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea/
2+
.vscode/
3+
.git/
4+
node_modules/
5+
.next/
6+
.github/
7+
coverage/
8+
dist/
9+
logs/

.env.example

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@ EMAIL_FROM=
1313
NEXTAUTH_URL=
1414
NEXTAUTH_SECRET=
1515
NEXT_PUBLIC_BASE_URL=
16+
17+
# LOCAL
1618
DATABASE_USER=
19+
DATABASE_URL=
1720
NEXT_PUBLIC_DATABASE_HOST=
1821
NEXT_PUBLIC_DATABASE_PORT=
19-
DATABASE_URL=
22+
23+
# DOCKER PRODUCTION
24+
# DATABASE_USER=
25+
# NEXT_PUBLIC_DATABASE_HOST=
26+
# NEXT_PUBLIC_DATABASE_PORT=
27+
# PGPASSWORD=
28+
# PGDATABASE=
29+
# DATABASE_URL=
30+
31+
32+
# VERCEL PRODUCTION
33+
# DATABASE_USER=
34+
# NEXT_PUBLIC_DATABASE_HOST=
35+
# NEXT_PUBLIC_DATABASE_PORT=
36+
# DATABASE_URL=

Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM node:18-alpine AS base
2+
3+
FROM base AS deps
4+
RUN apk add --no-cache libc6-compat
5+
WORKDIR /app
6+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
7+
RUN \
8+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
9+
elif [ -f package-lock.json ]; then npm ci; \
10+
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
11+
else echo "Lockfile not found." && exit 1; \
12+
fi
13+
RUN npm install -g --arch=x64 --platform=linux --libc=glibc sharp
14+
15+
FROM base AS builder
16+
WORKDIR /app
17+
COPY . .
18+
COPY --from=deps /app/node_modules ./node_modules
19+
ENV BUILD_STANDALONE 1
20+
ENV NEXT_TELEMETRY_DISABLED 1
21+
RUN npm run build
22+
23+
FROM base AS runner
24+
WORKDIR /app
25+
ENV NODE_ENV production
26+
ENV NEXT_SHARP_PATH=/usr/local/lib/node_modules/sharp
27+
RUN addgroup --system --gid 1001 nodejs
28+
RUN adduser --system --uid 1001 nextjs
29+
COPY --from=deps --chown=nextjs:nodejs /usr/local/lib/node_modules/sharp /usr/local/lib/node_modules/sharp
30+
COPY --from=builder /app/public ./public
31+
RUN mkdir dist
32+
RUN chown nextjs:nodejs dist
33+
COPY --from=builder --chown=nextjs:nodejs /app/dist/standalone ./
34+
COPY --from=builder --chown=nextjs:nodejs /app/dist/static ./dist/static
35+
36+
USER nextjs
37+
EXPOSE 3000
38+
ENV PORT 3000
39+
ENV HOSTNAME "0.0.0.0"
40+
41+
CMD ["node", "server.js"]

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,32 @@ Finally, you can run the project with the following command:
8181
```bash
8282
npm run start
8383
```
84+
85+
## Docker
86+
87+
The project can also be run using `docker-compose`. To run the project using `docker-compose`, you need to create a `.env` file in the root directory of the project and add above mentioned variables.
88+
89+
Set the following additional variables in the `.env` file:
90+
91+
```bash
92+
PGPASSWORD= # Database password
93+
PGDATABASE= # Database name
94+
```
95+
96+
This is required to spin up the database container. You don't need to create the database manually as it will be created automatically by the database container.
97+
98+
To run the project using `docker-compose`, you can use the following command:
99+
100+
```bash
101+
docker compose up production --build
102+
```
103+
104+
To create the required tables, you can use the following command:
105+
106+
```bash
107+
cat ./src/bin/tables.sql | docker exec -i datacanvas_db psql -U <DATABASE_USER> -d <PGDATABASE>
108+
```
109+
110+
# Contributing
111+
112+
Contributions are always welcome! Feel free to open any issues or send pull requests.

docker-compose.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
services:
2+
database:
3+
container_name: datacanvas_db
4+
image: postgres:16.1-alpine
5+
restart: always
6+
profiles:
7+
- production
8+
volumes:
9+
- postgres_data:/var/lib/postgresql/data
10+
environment:
11+
- POSTGRES_USER=${DATABASE_USER}
12+
- POSTGRES_PASSWORD=${PGPASSWORD}
13+
- POSTGRES_DB=${PGDATABASE}
14+
ports:
15+
- "5433:5432"
16+
production:
17+
build:
18+
context: .
19+
dockerfile: Dockerfile
20+
container_name: datacanvas
21+
restart: always
22+
depends_on:
23+
- database
24+
links:
25+
- database
26+
profiles:
27+
- production
28+
ports:
29+
- "3000:3000"
30+
31+
volumes:
32+
postgres_data:

next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const nextConfig = {
1212
},
1313
],
1414
},
15+
output: process.env.BUILD_STANDALONE ? "standalone" : undefined,
1516
reactStrictMode: true,
1617
distDir: "dist",
1718
};

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@types/react-responsive-masonry": "^2.1.3",
3434
"@typescript-eslint/eslint-plugin": "^5.59.6",
3535
"@typescript-eslint/parser": "^5.59.6",
36-
"autoprefixer": "^10.4.14",
36+
"autoprefixer": "^10.4.17",
3737
"eslint": "^7.32.0",
3838
"eslint-config-prettier": "^7.2.0",
3939
"eslint-import-resolver-typescript": "^3.6.1",

src/lib/db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class Database {
2727
private constructor() {
2828
this.pool = new pg.Pool({
2929
connectionString: process.env.DATABASE_URL,
30-
ssl: !process.env.DATABASE_URL?.includes("localhost"),
30+
ssl: !process.env.DATABASE_URL?.includes("localhost") && !process.env.DATABASE_URL?.includes("docker"),
3131
});
3232
}
3333

@@ -45,7 +45,7 @@ export default class Database {
4545
}
4646
const client = new pg.Client({
4747
connectionString: url,
48-
ssl: !process.env.DATABASE_URL?.includes("localhost"),
48+
ssl: !process.env.DATABASE_URL?.includes("localhost") && !process.env.DATABASE_URL?.includes("docker"),
4949
});
5050
await client.connect();
5151
Database.clientPool[session.user.id] = client;

0 commit comments

Comments
 (0)