Skip to content

Commit e281374

Browse files
committed
docker prod
1 parent 13fefc4 commit e281374

File tree

9 files changed

+116
-33
lines changed

9 files changed

+116
-33
lines changed

.dockerignore

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
node_modules/
2-
.next/
3-
.build/
4-
5-
npm-debug.log*
6-
yarn-debug.log*
7-
yarn-error.log*
1+
Dockerfile
2+
.dockerignore
3+
node_modules
4+
npm-debug.log
5+
README.md
6+
.next
7+
.git

.env

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# DO NOT ADD SECRETS TO THIS FILE. This is a good place for defaults.
22
# If you want to add secrets use `.env.local` instead.
33

4-
ENV_VARIABLE="server_only_variable"
5-
NEXT_PUBLIC_ENV_VARIABLE="public_variable"
6-
DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
4+
ENV_VARIABLE=production_server_only_variable
5+
NEXT_PUBLIC_ENV_VARIABLE=production_public_variable
6+
DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
7+
PORT=3000

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
DEVELOPMENT_ENV_VARIABLE="server_only_development_variable"
55
NEXT_PUBLIC_DEVELOPMENT_ENV_VARIABLE="public_development_variable"
6+
PORT=3000

.env.local.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
MONGODB_URI=
22
ENV_LOCAL_VARIABLE="server_only_variable_from_env_local"
3-
NEXT_PUBLIC_ENV_LOCAL_VARIABLE="public_variable_from_env_local"
3+
NEXT_PUBLIC_ENV_LOCAL_VARIABLE="public_variable_from_env_local"
4+
PORT=3000

.env.production

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
PRODUCTION_ENV_VARIABLE="server_only_production_variable"
55
NEXT_PUBLIC_PRODUCTION_ENV_VARIABLE="public_production_variable"
6-
DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
6+
DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
7+
PORT=3000

Dockerfile

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,73 @@
1-
FROM node:18-alpine
1+
FROM node:18-alpine AS base
22

3-
RUN mkdir -p /usr/src/app
3+
# Step 1. Rebuild the source code only when needed
4+
FROM base AS builder
45

5-
WORKDIR /usr/src/app
6+
WORKDIR /app
67

7-
COPY package.json /usr/src/app/
8+
# Install dependencies based on the preferred package manager
9+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
10+
# Omit --production flag for TypeScript devDependencies
11+
RUN \
12+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
13+
elif [ -f package-lock.json ]; then npm ci; \
14+
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
15+
# Allow install without lockfile, so example works even without Node.js installed locally
16+
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
17+
fi
818

9-
RUN npm install -g next
19+
COPY . .
20+
COPY public ./public
21+
COPY next.config.js .
22+
COPY tsconfig.json .
1023

11-
RUN npm install
24+
# Environment variables must be present at build time
25+
# https://github.com/vercel/next.js/discussions/14030
26+
ARG ENV_VARIABLE
27+
ENV ENV_VARIABLE=${ENV_VARIABLE}
28+
ARG NEXT_PUBLIC_ENV_VARIABLE
29+
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
1230

13-
COPY . /usr/src/app
31+
# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
32+
# Uncomment the following line to disable telemetry at build time
33+
# ENV NEXT_TELEMETRY_DISABLED 1
1434

15-
RUN npm run build
35+
# Build Next.js based on the preferred package manager
36+
RUN \
37+
if [ -f yarn.lock ]; then yarn build; \
38+
elif [ -f package-lock.json ]; then npm run build; \
39+
elif [ -f pnpm-lock.yaml ]; then pnpm build; \
40+
else yarn build; \
41+
fi
1642

17-
COPY --chown=10101 ./public /usr/src/app/public
43+
# Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here
1844

19-
ENV DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
45+
# Step 2. Production image, copy all the files and run next
46+
FROM base AS runner
2047

21-
EXPOSE 3000
48+
WORKDIR /app
49+
50+
# Don't run production as root
51+
RUN addgroup --system --gid 1001 nodejs
52+
RUN adduser --system --uid 1001 nextjs
53+
USER nextjs
54+
55+
COPY --from=builder /app/public ./public
56+
57+
# Automatically leverage output traces to reduce image size
58+
# https://nextjs.org/docs/advanced-features/output-file-tracing
59+
# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
60+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
61+
62+
# Environment variables must be redefined at run time
63+
ARG ENV_VARIABLE
64+
ENV ENV_VARIABLE=${ENV_VARIABLE}
65+
ARG NEXT_PUBLIC_ENV_VARIABLE
66+
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
67+
68+
# Uncomment the following line to disable telemetry at run time
69+
# ENV NEXT_TELEMETRY_DISABLED 1
70+
71+
# Note: Don't expose ports here, Compose will handle that for us
2272

2373
CMD npm run launch

develop.Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:18-alpine
2+
3+
RUN mkdir -p /usr/src/app
4+
5+
WORKDIR /usr/src/app
6+
7+
COPY package.json /usr/src/app/
8+
9+
RUN npm install -g next
10+
11+
RUN npm install
12+
13+
COPY . /usr/src/app
14+
15+
RUN npm run build
16+
17+
COPY --chown=10101 ./public /usr/src/app/public
18+
19+
ENV DB_LOCAL_URL=mongodb://mongo:27017/denizpaz
20+
21+
EXPOSE 3000
22+
23+
CMD npm run launch

docker-compose.yml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
version: "3.8"
22
services:
33
web:
4-
build: .
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
args:
8+
ENV_VARIABLE: ${ENV_VARIABLE}
9+
NEXT_PUBLIC_ENV_VARIABLE: ${NEXT_PUBLIC_ENV_VARIABLE}
510
volumes:
6-
- .:/usr/src/app
7-
- /usr/src/app/node_modules
8-
- /usr/src/app/.next
9-
- /usr/src/app/public
11+
- .:/app
12+
- ./public:/app/public
13+
# - /app/node_modules
14+
# - /app/.next
1015
command: npm run launch
16+
restart: always
1117
ports:
12-
- "3000:3000"
18+
- "${PORT}:${PORT}"
1319
environment:
14-
NODE_ENV: production
1520
DB_LOCAL_URL: mongodb://mongo:27017/denizpazs
1621
depends_on:
1722
- mongo
@@ -30,8 +35,8 @@ services:
3035

3136
networks:
3237
asgard:
33-
ipam:
34-
driver: default
38+
external: true
3539

3640
volumes:
3741
data:
42+
driver: local

next.config.js

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

67
module.exports = nextConfig

0 commit comments

Comments
 (0)