+
+
Next.js can be deployed to any hosting provider that supports [Docker](https://www.docker.com/) containers. You can use this approach when deploying to container orchestrators such as [Kubernetes](https://kubernetes.io/) or [HashiCorp Nomad](https://www.nomadproject.io/), or when running inside a single node in any cloud provider.
Here is a multi-stage `Dockerfile` using `node:alpine` that you can use:
@@ -111,6 +118,7 @@ ENV NODE_ENV production
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
+COPY --from=builder /app/package.json ./package.json
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
@@ -124,7 +132,7 @@ EXPOSE 3000
# Uncomment the following line in case you want to disable telemetry.
# RUN npx next telemetry disable
-CMD ["node_modules/.bin/next", "start"]
+CMD ["yarn", "start"]
```
Make sure to place this Dockerfile in the root folder of your project.
diff --git a/examples/with-docker/.dockerignore b/examples/with-docker/.dockerignore
index fabc1be3226b..d862f96f1b93 100644
--- a/examples/with-docker/.dockerignore
+++ b/examples/with-docker/.dockerignore
@@ -1,3 +1,6 @@
-.next/
-node_modules/
Dockerfile
+.dockerignore
+node_modules
+npm-debug.log
+README.md
+.next
\ No newline at end of file
diff --git a/examples/with-docker/Dockerfile b/examples/with-docker/Dockerfile
index 2d17c906a625..b08d4619a839 100644
--- a/examples/with-docker/Dockerfile
+++ b/examples/with-docker/Dockerfile
@@ -1,12 +1,35 @@
-FROM mhart/alpine-node
-
+# Install dependencies only when needed
+FROM node:alpine AS deps
+# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
+RUN apk add --no-cache libc6-compat
WORKDIR /app
+COPY package.json yarn.lock ./
+RUN yarn install --frozen-lockfile
+# Rebuild the source code only when needed
+FROM node:alpine AS builder
+WORKDIR /app
COPY . .
+COPY --from=deps /app/node_modules ./node_modules
+RUN yarn build
-RUN yarn install --frozen-lockfile
+# Production image, copy all the files and run next
+FROM node:alpine AS runner
+WORKDIR /app
-RUN yarn build
+ENV NODE_ENV production
+
+# You only need to copy next.config.js if you are NOT using the default configuration
+# COPY --from=builder /app/next.config.js ./
+COPY --from=builder /app/public ./public
+COPY --from=builder /app/.next ./.next
+COPY --from=builder /app/node_modules ./node_modules
+COPY --from=builder /app/package.json ./package.json
+
+RUN addgroup -g 1001 -S nodejs
+RUN adduser -S nextjs -u 1001
+RUN chown -R nextjs:nodejs /app/.next
+USER nextjs
EXPOSE 3000
diff --git a/examples/with-docker/Dockerfile.multistage b/examples/with-docker/Dockerfile.multistage
deleted file mode 100644
index a69f05c2c652..000000000000
--- a/examples/with-docker/Dockerfile.multistage
+++ /dev/null
@@ -1,28 +0,0 @@
-# Stage 1: Building the code
-FROM mhart/alpine-node AS builder
-
-WORKDIR /app
-
-COPY package.json yarn.lock ./
-
-RUN yarn install --frozen-lockfile
-
-COPY . .
-
-RUN yarn build
-RUN yarn install --production --frozen-lockfile
-
-
-# Stage 2: And then copy over node_modules, etc from that stage to the smaller base image
-FROM mhart/alpine-node:slim as production
-
-WORKDIR /app
-
-# COPY package.json next.config.js .env* ./
-COPY --from=builder /app/public ./public
-COPY --from=builder /app/.next ./.next
-COPY --from=builder /app/node_modules ./node_modules
-
-EXPOSE 3000
-
-CMD ["node_modules/.bin/next", "start"]
\ No newline at end of file
diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md
index 055dd2881be6..de4ba79fcdde 100644
--- a/examples/with-docker/README.md
+++ b/examples/with-docker/README.md
@@ -1,42 +1,53 @@
# With Docker
-This example shows how to set custom environment variables for your **docker application** at runtime.
-
-The `dockerfile` is the simplest way to run Next.js app in docker, and the size of output image is `173MB`. However, for an even smaller build, you can do multi-stage builds with `dockerfile.multistage`. The size of output image is `85MB`.
-
-You can check the [Example Dockerfile for your own Node.js project](https://github.com/mhart/alpine-node/tree/43ca9e4bc97af3b1f124d27a2cee002d5f7d1b32#example-dockerfile-for-your-own-nodejs-project) section in [mhart/alpine-node](https://github.com/mhart/alpine-node) for more details.
+This examples shows how to use Docker with Next.js based on the [deployment documentation](https://nextjs.org/docs/deployment#docker-image). Additionally, it contains instructions for deploying to Google Cloud Run. However, you can use any container-based deployment host.
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
-npx create-next-app --example with-docker with-docker-app
+npx create-next-app --example with-docker nextjs-docker
# or
-yarn create next-app --example with-docker with-docker-app
+yarn create next-app --example with-docker nextjs-docker
```
-Build it with docker:
+## Using Docker
-```bash
-# build
-docker build -t next-app .
-# or, use multi-stage builds to build a smaller docker image
-docker build --target production -t next-app -f ./Dockerfile.multistage .
-```
+1. [Install Docker](https://docs.docker.com/get-docker/) on your machine.
+1. Build your container: `docker build . -t nextjs-docker`.
+1. Run your container: `docker run -p 3000:3000 nextjs-docker`.
+
+You can view your images created with `docker images`.
+
+## Deploying to Google Cloud Run
-Alternatively you can add these commands as scripts to your package.json and simply run
+The `start` script in `package.json` has been modified to accept a `PORT` environment variable (for compatability with Google Cloud Run).
-`yarn build-docker`
-or
-`yarn build-docker-multistage`
+1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) so you can use `gcloud` on the command line.
+1. Run `gcloud auth login` to log in to your account.
+1. [Create a new project](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) in Google Cloud Run (e.g. `nextjs-docker`). Ensure billing is turned on.
+1. Build your container image using Cloud Build: `gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld --project PROJECT-ID`. This will also enable Cloud Build for your project.
+1. Deploy to Cloud Run: `gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --project PROJECT-ID --platform managed`. Choose a region of your choice.
-Run the docker image:
+ - You will be prompted for the service name: press Enter to accept the default name, `helloworld`.
+ - You will be prompted for [region](https://cloud.google.com/run/docs/quickstarts/build-and-deploy#follow-cloud-run): select the region of your choice, for example `us-central1`.
+ - You will be prompted to **allow unauthenticated invocations**: respond `y`.
+
+## Running Locally
+
+First, run the development server:
```bash
-docker run --rm -it \
- -p 3000:3000 \
- next-app
+npm run dev
+# or
+yarn dev
```
-or use `yarn build-docker-multistage`
+Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
+
+You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
+
+[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
+
+The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
diff --git a/examples/with-docker/package.json b/examples/with-docker/package.json
index 388a8f406837..c4fc532c84a3 100644
--- a/examples/with-docker/package.json
+++ b/examples/with-docker/package.json
@@ -2,17 +2,14 @@
"name": "with-docker",
"version": "1.0.0",
"scripts": {
- "dev": "next",
+ "dev": "next dev",
"build": "next build",
- "start": "next start",
- "build-docker": "docker build -t next-app .",
- "build-docker-multistage": "docker build --target production -t next-app -f ./Dockerfile.multistage .",
- "run-docker": "docker run --rm -it -p 3000:3000 next-app"
+ "start": "next start -p $PORT"
},
"dependencies": {
"next": "latest",
- "react": "^16.7.0",
- "react-dom": "^16.7.0"
+ "react": "^17.0.2",
+ "react-dom": "^17.0.2"
},
"license": "MIT"
}
diff --git a/examples/with-docker/pages/_app.js b/examples/with-docker/pages/_app.js
new file mode 100644
index 000000000000..1e1cec92425c
--- /dev/null
+++ b/examples/with-docker/pages/_app.js
@@ -0,0 +1,7 @@
+import '../styles/globals.css'
+
+function MyApp({ Component, pageProps }) {
+ return
+}
+
+export default MyApp
diff --git a/examples/with-docker/pages/api/hello.js b/examples/with-docker/pages/api/hello.js
new file mode 100644
index 000000000000..441a0a1006e5
--- /dev/null
+++ b/examples/with-docker/pages/api/hello.js
@@ -0,0 +1,5 @@
+// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
+
+export default function hello(req, res) {
+ res.status(200).json({ name: 'John Doe' })
+}
diff --git a/examples/with-docker/pages/index.js b/examples/with-docker/pages/index.js
index 557f90a54450..5787b11aa7f1 100644
--- a/examples/with-docker/pages/index.js
+++ b/examples/with-docker/pages/index.js
@@ -1,8 +1,65 @@
+import Head from 'next/head'
+import styles from '../styles/Home.module.css'
+
export default function Home() {
return (
- <>
-