|
| 1 | +--- |
| 2 | +title: Docker |
| 3 | +description: Deployment with Docker |
| 4 | +layout: ../../../layouts/docs.astro |
| 5 | +lang: en |
| 6 | +--- |
| 7 | + |
| 8 | +You can containerize this stack and deploy it as a single container using Docker, or as a part of a group of containers using docker-compose. See [`ajcwebdev/ct3a-docker`](https://github.com/ajcwebdev/ct3a-docker) for an example repo based on this doc. |
| 9 | + |
| 10 | +## Docker Project Configuration |
| 11 | + |
| 12 | +Please note that Next.js requires a different process for build time (available in the frontend, prefixed by `NEXT_PUBLIC`) and runtime environment, server-side only, variables. In this demo we are using two variables, pay attention to their positions in the `Dockerfile`, command-line arguments, and `docker-compose.yml`: |
| 13 | + |
| 14 | +- `DATABASE_URL` (used by the server) |
| 15 | +- `NEXT_PUBLIC_CLIENTVAR` (used by the client) |
| 16 | + |
| 17 | +### 1. Next Configuration |
| 18 | + |
| 19 | +In your [`next.config.js`](https://github.com/t3-oss/create-t3-app/blob/main/cli/template/base/next.config.js), add the `standalone` output-option configuration to [reduce image size by automatically leveraging output traces](https://nextjs.org/docs/advanced-features/output-file-tracing): |
| 20 | + |
| 21 | +```diff |
| 22 | +export default defineNextConfig({ |
| 23 | + reactStrictMode: true, |
| 24 | + swcMinify: true, |
| 25 | ++ output: "standalone", |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Create dockerignore file |
| 30 | + |
| 31 | +<details> |
| 32 | + <summary> |
| 33 | + Click here and include contents in <code>.dockerignore</code>: |
| 34 | + </summary> |
| 35 | +<div class="content"> |
| 36 | + |
| 37 | +``` |
| 38 | +.env |
| 39 | +Dockerfile |
| 40 | +.dockerignore |
| 41 | +node_modules |
| 42 | +npm-debug.log |
| 43 | +README.md |
| 44 | +.next |
| 45 | +.git |
| 46 | +``` |
| 47 | + |
| 48 | +</div> |
| 49 | + |
| 50 | +</details> |
| 51 | + |
| 52 | +### 3. Create Dockerfile |
| 53 | + |
| 54 | +> Since we're not pulling the server environment variables into our container, the [environment schema validation](/en/usage/env-variables) will fail. To prevent this, we have to add a `SKIP_ENV_VALIDATION=1` flag to the build command so that the env-schemas aren't validated at build time. |
| 55 | +
|
| 56 | +<details> |
| 57 | + <summary> |
| 58 | + Click here and include contents in <code>Dockerfile</code>: |
| 59 | + </summary> |
| 60 | +<div class="content"> |
| 61 | + |
| 62 | +```docker |
| 63 | +##### DEPENDENCIES |
| 64 | +
|
| 65 | +FROM --platform=linux/amd64 node:20-alpine AS deps |
| 66 | +RUN apk add --no-cache libc6-compat openssl |
| 67 | +WORKDIR /app |
| 68 | +
|
| 69 | +# Install Prisma Client - remove if not using Prisma |
| 70 | +
|
| 71 | +COPY prisma ./ |
| 72 | +
|
| 73 | +# Install dependencies based on the preferred package manager |
| 74 | +
|
| 75 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml\* ./ |
| 76 | +
|
| 77 | +RUN \ |
| 78 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 79 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 80 | + elif [ -f pnpm-lock.yaml ]; then npm install -g pnpm && pnpm i; \ |
| 81 | + else echo "Lockfile not found." && exit 1; \ |
| 82 | + fi |
| 83 | +
|
| 84 | +##### BUILDER |
| 85 | +
|
| 86 | +FROM --platform=linux/amd64 node:20-alpine AS builder |
| 87 | +ARG DATABASE_URL |
| 88 | +ARG NEXT_PUBLIC_CLIENTVAR |
| 89 | +WORKDIR /app |
| 90 | +COPY --from=deps /app/node_modules ./node_modules |
| 91 | +COPY . . |
| 92 | +
|
| 93 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 94 | +
|
| 95 | +RUN \ |
| 96 | + if [ -f yarn.lock ]; then SKIP_ENV_VALIDATION=1 yarn build; \ |
| 97 | + elif [ -f package-lock.json ]; then SKIP_ENV_VALIDATION=1 npm run build; \ |
| 98 | + elif [ -f pnpm-lock.yaml ]; then npm install -g pnpm && SKIP_ENV_VALIDATION=1 pnpm run build; \ |
| 99 | + else echo "Lockfile not found." && exit 1; \ |
| 100 | + fi |
| 101 | +
|
| 102 | +##### RUNNER |
| 103 | +
|
| 104 | +FROM --platform=linux/amd64 gcr.io/distroless/nodejs20-debian12 AS runner |
| 105 | +WORKDIR /app |
| 106 | +
|
| 107 | +ENV NODE_ENV production |
| 108 | +
|
| 109 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 110 | +
|
| 111 | +COPY --from=builder /app/next.config.js ./ |
| 112 | +COPY --from=builder /app/public ./public |
| 113 | +COPY --from=builder /app/package.json ./package.json |
| 114 | +
|
| 115 | +COPY --from=builder /app/.next/standalone ./ |
| 116 | +COPY --from=builder /app/.next/static ./.next/static |
| 117 | +
|
| 118 | +EXPOSE 3000 |
| 119 | +ENV PORT 3000 |
| 120 | +
|
| 121 | +CMD ["server.js"] |
| 122 | +``` |
| 123 | + |
| 124 | +> **_Notes_** |
| 125 | +> |
| 126 | +> - _Emulation of `--platform=linux/amd64` may not be necessary after moving to Node 18._ |
| 127 | +> - _See [`node:alpine`](https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine) to understand why `libc6-compat` might be needed._ |
| 128 | +> - _Using Alpine 3.17 based images [can cause issues with Prisma](https://github.com/t3-oss/create-t3-app/issues/975). Setting `engineType = "binary"` solves the issue in Alpine 3.17, [but has an associated performance cost](https://www.prisma.io/docs/concepts/components/prisma-engines/query-engine#the-query-engine-at-runtime)._ |
| 129 | +> - _Next.js collects [anonymous telemetry data about general usage](https://nextjs.org/telemetry). Uncomment the first instance of `ENV NEXT_TELEMETRY_DISABLED 1` to disable telemetry during the build. Uncomment the second instance to disable telemetry during runtime._ |
| 130 | +
|
| 131 | +</div> |
| 132 | +</details> |
| 133 | + |
| 134 | +## Build and Run Image Locally |
| 135 | + |
| 136 | +Build and run this image locally with the following commands: |
| 137 | + |
| 138 | +```bash |
| 139 | +docker build -t ct3a-docker --build-arg NEXT_PUBLIC_CLIENTVAR=clientvar . |
| 140 | +docker run -p 3000:3000 -e DATABASE_URL="database_url_goes_here" ct3a-docker |
| 141 | +``` |
| 142 | + |
| 143 | +Open [localhost:3000](http://localhost:3000/) to see your running application. |
| 144 | + |
| 145 | +## Docker Compose |
| 146 | + |
| 147 | +You can also use Docker Compose to build the image and run the container. |
| 148 | + |
| 149 | +<details> |
| 150 | + <summary> |
| 151 | + Follow steps 1-3 above, click here, and include contents in <code>docker-compose.yml</code>: |
| 152 | + </summary> |
| 153 | +<div class="content"> |
| 154 | + |
| 155 | +```yaml |
| 156 | +version: "3.9" |
| 157 | +services: |
| 158 | + app: |
| 159 | + platform: "linux/amd64" |
| 160 | + build: |
| 161 | + context: . |
| 162 | + dockerfile: Dockerfile |
| 163 | + args: |
| 164 | + NEXT_PUBLIC_CLIENTVAR: "clientvar" |
| 165 | + working_dir: /app |
| 166 | + ports: |
| 167 | + - "3000:3000" |
| 168 | + image: t3-app |
| 169 | + environment: |
| 170 | + - DATABASE_URL=database_url_goes_here |
| 171 | +``` |
| 172 | +
|
| 173 | +Build and run this using the `docker compose up --build` command: |
| 174 | + |
| 175 | +```bash |
| 176 | +docker compose up --build |
| 177 | +``` |
| 178 | + |
| 179 | +Open [localhost:3000](http://localhost:3000/) to see your running application. |
| 180 | + |
| 181 | +</div> |
| 182 | +</details> |
| 183 | + |
| 184 | +## Deploy to Railway |
| 185 | + |
| 186 | +You can use a PaaS such as [Railway's](https://railway.app) automated [Dockerfile deployments](https://docs.railway.app/deploy/dockerfiles) to deploy your app. If you have the [Railway CLI installed](https://docs.railway.app/develop/cli#install) you can deploy your app with the following commands: |
| 187 | + |
| 188 | +```bash |
| 189 | +railway login |
| 190 | +railway init |
| 191 | +railway link |
| 192 | +railway up |
| 193 | +railway open |
| 194 | +``` |
| 195 | + |
| 196 | +Go to "Variables" and include your `DATABASE_URL`. Then go to "Settings" and select "Generate Domain." To view a running example on Railway, visit [ct3a-docker.up.railway.app](https://ct3a-docker.up.railway.app/). |
| 197 | + |
| 198 | +## Useful Resources |
| 199 | + |
| 200 | +| Resource | Link | |
| 201 | +| ------------------------------------ | -------------------------------------------------------------------- | |
| 202 | +| Dockerfile reference | https://docs.docker.com/engine/reference/builder/ | |
| 203 | +| Compose file version 3 reference | https://docs.docker.com/compose/compose-file/compose-file-v3/ | |
| 204 | +| Docker CLI reference | https://docs.docker.com/engine/reference/commandline/docker/ | |
| 205 | +| Docker Compose CLI reference | https://docs.docker.com/compose/reference/ | |
| 206 | +| Next.js Deployment with Docker Image | https://nextjs.org/docs/deployment#docker-image | |
| 207 | +| Next.js in Docker | https://benmarte.com/blog/nextjs-in-docker/ | |
| 208 | +| Next.js with Docker Example | https://github.com/vercel/next.js/tree/canary/examples/with-docker | |
| 209 | +| Create Docker Image of a Next.js app | https://blog.tericcabrel.com/create-docker-image-nextjs-application/ | |
0 commit comments