Skip to content

Commit 83b47d5

Browse files
authored
Merge pull request #25 from CS3219-AY2425S1/ben/frontend-docker
feat: Add Dockerfile for both frontend and user-service
2 parents 8b00680 + f79c4b8 commit 83b47d5

File tree

8 files changed

+2090
-2300
lines changed

8 files changed

+2090
-2300
lines changed

apps/frontend/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
.dockerignore
3+
.git
4+
.gitignore
5+
.next
6+
out
7+
public
8+
README.md

apps/frontend/Dockerfile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
FROM node:18-alpine AS base
2+
3+
# For now we can specify it here, else we can use --build-arg in our docker build command
4+
ARG NEXT_PUBLIC_API_URL
5+
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
6+
7+
# Install dependencies only when needed
8+
FROM base AS deps
9+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
10+
RUN apk add --no-cache libc6-compat
11+
WORKDIR /app
12+
13+
# Install dependencies based on the preferred package manager
14+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
15+
RUN \
16+
if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
17+
else echo "Lockfile not found." && exit 1; \
18+
fi
19+
20+
# Rebuild the source code only when needed
21+
FROM base AS builder
22+
WORKDIR /app
23+
COPY --from=deps /app/node_modules ./node_modules
24+
COPY . .
25+
26+
# Next.js collects completely anonymous telemetry data about general usage.
27+
# Learn more here: https://nextjs.org/telemetry
28+
# Uncomment the following line in case you want to disable telemetry during the build.
29+
# ENV NEXT_TELEMETRY_DISABLED=1
30+
31+
RUN \
32+
if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
33+
else echo "Lockfile not found." && exit 1; \
34+
fi
35+
36+
# Production image, copy all the files and run next
37+
FROM base AS runner
38+
WORKDIR /app
39+
40+
ENV NODE_ENV=production
41+
# Uncomment the following line in case you want to disable telemetry during runtime.
42+
# ENV NEXT_TELEMETRY_DISABLED=1
43+
44+
RUN addgroup --system --gid 1001 nodejs
45+
RUN adduser --system --uid 1001 nextjs
46+
47+
# Include this when we want to include images in the future
48+
# COPY --from=builder /app/public ./public
49+
50+
# Set the correct permission for prerender cache
51+
RUN mkdir .next
52+
RUN chown nextjs:nodejs .next
53+
54+
# Automatically leverage output traces to reduce image size
55+
# https://nextjs.org/docs/advanced-features/output-file-tracing
56+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
57+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
58+
59+
USER nextjs
60+
61+
EXPOSE 3000
62+
63+
ENV PORT=3000
64+
65+
# server.js is created by next build from the standalone output
66+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
67+
ENV HOSTNAME="0.0.0.0"
68+
CMD ["node", "server.js"]

apps/frontend/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,26 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the
3636

3737
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
3838

39-
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
39+
## Build Dockerfile
40+
41+
```sh
42+
# Navigate to the frontend app directory
43+
cd apps/frontend
44+
# Build dockerfile (Ensure that your docker daemon is running beforehand)
45+
docker build -t frontend --build-arg NEXT_PUBLIC_API_URL="http://localhost:8080/" -f Dockerfile .
46+
```
47+
48+
Run the backend server locally and visit http://localhost:3000/ to see the frontend application working
49+
50+
## Running Docker Image
51+
52+
```sh
53+
# Run the docker image, the -d tag is to run it detached
54+
docker run -p 3000:3000 -d frontend
55+
56+
# To see the running container
57+
docker ps
58+
59+
# To stop the container, copy the container id from the previous command
60+
docker stop <container_id>
61+
```

apps/frontend/next.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {};
2+
const nextConfig = {
3+
output: "standalone",
4+
};
35

46
export default nextConfig;

0 commit comments

Comments
 (0)