Skip to content

Commit 37bb958

Browse files
authored
docs: add instructions on building docker image to readme (#265)
* Add instructions on building docker image Following the same method given in NextJs examples * fix: prettier check run prettier format to fix formatting issues
1 parent c7d4dce commit 37bb958

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

template/base/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,86 @@ We recommend deploying to [Vercel](https://vercel.com/?utm_source=t3-oss&utm_cam
3030
- Click **Deploy**
3131
- Now whenever you push a change to your repository, Vercel will automatically redeploy your website!
3232

33+
You can also dockerize this stack and deploy a container.
34+
35+
- In your next.config.mjs, add the `output: "standalone"` option to your config.
36+
- Create a `.dockerignore` file with the following contents:
37+
```
38+
Dockerfile
39+
.dockerignore
40+
node_modules
41+
npm-debug.log
42+
README.md
43+
.next
44+
.git
45+
```
46+
- Create a `Dockerfile` with the following contents:
47+
48+
```
49+
# Install dependencies only when needed
50+
FROM node:16-alpine AS deps
51+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
52+
RUN apk add --no-cache libc6-compat
53+
WORKDIR /app
54+
55+
# Install dependencies based on the preferred package manager
56+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
57+
RUN \
58+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
59+
elif [ -f package-lock.json ]; then npm ci; \
60+
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
61+
else echo "Lockfile not found." && exit 1; \
62+
fi
63+
64+
65+
# Rebuild the source code only when needed
66+
FROM node:16-alpine AS builder
67+
WORKDIR /app
68+
COPY --from=deps /app/node_modules ./node_modules
69+
COPY . .
70+
71+
# Next.js collects completely anonymous telemetry data about general usage.
72+
# Learn more here: https://nextjs.org/telemetry
73+
# Uncomment the following line in case you want to disable telemetry during the build.
74+
# ENV NEXT_TELEMETRY_DISABLED 1
75+
76+
RUN yarn build
77+
78+
# If using npm comment out above and use below instead
79+
# RUN npm run build
80+
81+
# Production image, copy all the files and run next
82+
FROM node:16-alpine AS runner
83+
WORKDIR /app
84+
85+
ENV NODE_ENV production
86+
# Uncomment the following line in case you want to disable telemetry during runtime.
87+
# ENV NEXT_TELEMETRY_DISABLED 1
88+
89+
RUN addgroup --system --gid 1001 nodejs
90+
RUN adduser --system --uid 1001 nextjs
91+
92+
# You only need to copy next.config.js if you are NOT using the default configuration
93+
# COPY --from=builder /app/next.config.js ./
94+
COPY --from=builder /app/public ./public
95+
COPY --from=builder /app/package.json ./package.json
96+
97+
# Automatically leverage output traces to reduce image size
98+
# https://nextjs.org/docs/advanced-features/output-file-tracing
99+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
100+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
101+
102+
USER nextjs
103+
104+
EXPOSE 3000
105+
106+
ENV PORT 3000
107+
108+
CMD ["node", "server.js"]
109+
```
110+
111+
- You can now build an image to deploy yourself, or use a PaaS such as [Railway's](https://railway.app) automated [Dockerfile deployments](https://docs.railway.app/deploy/dockerfiles) to deploy your app.
112+
33113
## Useful resources
34114

35115
Here are some resources that we commonly refer to:

0 commit comments

Comments
 (0)