Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Update `with-docker` example and deployment docs. (#23486)
## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [x] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [x] Make sure the linting passes
- Loading branch information
Showing
with
294 additions
and 72 deletions.
- +9 −1 docs/deployment.md
- +5 −2 examples/with-docker/.dockerignore
- +27 −4 examples/with-docker/Dockerfile
- +0 −28 examples/with-docker/Dockerfile.multistage
- +34 −23 examples/with-docker/README.md
- +4 −7 examples/with-docker/package.json
- +7 −0 examples/with-docker/pages/_app.js
- +5 −0 examples/with-docker/pages/api/hello.js
- +61 −4 examples/with-docker/pages/index.js
- BIN examples/with-docker/public/favicon.ico
- +4 −3 examples/with-docker/public/vercel.svg
- +122 −0 examples/with-docker/styles/Home.module.css
- +16 −0 examples/with-docker/styles/globals.css
| @@ -1,3 +1,6 @@ | ||
| .next/ | ||
| node_modules/ | ||
| Dockerfile | ||
| .dockerignore | ||
| node_modules | ||
| npm-debug.log | ||
| README.md | ||
| .next |
| @@ -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. |
| @@ -0,0 +1,7 @@ | ||
| import '../styles/globals.css' | ||
|
|
||
| function MyApp({ Component, pageProps }) { | ||
| return <Component {...pageProps} /> | ||
| } | ||
|
|
||
| export default MyApp |
| @@ -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' }) | ||
| } |
| @@ -1,8 +1,65 @@ | ||
| import Head from 'next/head' | ||
| import styles from '../styles/Home.module.css' | ||
|
|
||
| export default function Home() { | ||
| return ( | ||
| <> | ||
| <h1>Hello World!</h1> | ||
| <img src="vercel.svg" alt="Vercel" /> | ||
| </> | ||
| <div className={styles.container}> | ||
| <Head> | ||
| <title>Create Next App</title> | ||
| <link rel="icon" href="/favicon.ico" /> | ||
| </Head> | ||
|
|
||
| <main className={styles.main}> | ||
| <h1 className={styles.title}> | ||
| Welcome to <a href="https://nextjs.org">Next.js!</a> | ||
| </h1> | ||
|
|
||
| <p className={styles.description}> | ||
| Get started by editing{' '} | ||
| <code className={styles.code}>pages/index.js</code> | ||
| </p> | ||
|
|
||
| <div className={styles.grid}> | ||
| <a href="https://nextjs.org/docs" className={styles.card}> | ||
| <h3>Documentation →</h3> | ||
| <p>Find in-depth information about Next.js features and API.</p> | ||
| </a> | ||
|
|
||
| <a href="https://nextjs.org/learn" className={styles.card}> | ||
| <h3>Learn →</h3> | ||
| <p>Learn about Next.js in an interactive course with quizzes!</p> | ||
| </a> | ||
|
|
||
| <a | ||
| href="https://github.com/vercel/next.js/tree/master/examples" | ||
| className={styles.card} | ||
| > | ||
| <h3>Examples →</h3> | ||
| <p>Discover and deploy boilerplate example Next.js projects.</p> | ||
| </a> | ||
|
|
||
| <a | ||
| href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" | ||
| className={styles.card} | ||
| > | ||
| <h3>Deploy →</h3> | ||
| <p> | ||
| Instantly deploy your Next.js site to a public URL with Vercel. | ||
| </p> | ||
| </a> | ||
| </div> | ||
| </main> | ||
|
|
||
| <footer className={styles.footer}> | ||
| <a | ||
| href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| Powered by{' '} | ||
| <img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} /> | ||
| </a> | ||
| </footer> | ||
| </div> | ||
| ) | ||
| } |
Binary file not shown.
| @@ -1,3 +1,4 @@ | ||
| <svg width="283" height="64" viewBox="0 0 283 64" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
| <path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000" /> | ||
| </svg> | ||
| <svg width="283" height="64" viewBox="0 0 283 64" fill="none" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/> | ||
| </svg> |
Oops, something went wrong.