Skip to content

Commit 965fda3

Browse files
author
Jacob Peddicord
committed
ok this proxy works
1 parent a7425b7 commit 965fda3

12 files changed

+633
-0
lines changed

Dockerfile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Install dependencies only when needed
2+
FROM node:16-alpine AS deps
3+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
4+
RUN apk add --no-cache libc6-compat
5+
WORKDIR /app
6+
COPY package.json yarn.lock ./
7+
RUN yarn install --frozen-lockfile
8+
9+
# If using npm with a `package-lock.json` comment out above and use below instead
10+
# COPY package.json package-lock.json ./
11+
# RUN npm ci
12+
13+
# Rebuild the source code only when needed
14+
FROM node:16-alpine AS builder
15+
WORKDIR /app
16+
COPY --from=deps /app/node_modules ./node_modules
17+
COPY . .
18+
19+
# Next.js collects completely anonymous telemetry data about general usage.
20+
# Learn more here: https://nextjs.org/telemetry
21+
# Uncomment the following line in case you want to disable telemetry during the build.
22+
# ENV NEXT_TELEMETRY_DISABLED 1
23+
24+
RUN yarn build
25+
26+
# Production image, copy all the files and run next
27+
FROM node:16-alpine AS runner
28+
WORKDIR /app
29+
30+
ENV NODE_ENV production
31+
# Uncomment the following line in case you want to disable telemetry during runtime.
32+
# ENV NEXT_TELEMETRY_DISABLED 1
33+
34+
RUN addgroup --system --gid 1001 nodejs
35+
RUN adduser --system --uid 1001 nextjs
36+
37+
# You only need to copy next.config.js if you are NOT using the default configuration
38+
# COPY --from=builder /app/next.config.js ./
39+
COPY --from=builder /app/public ./public
40+
COPY --from=builder /app/package.json ./package.json
41+
42+
# Automatically leverage output traces to reduce image size
43+
# https://nextjs.org/docs/advanced-features/output-file-tracing
44+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
45+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
46+
47+
USER nextjs
48+
49+
EXPOSE 3000
50+
51+
ENV PORT 3000
52+
53+
CMD ["node", "server.js"]

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# With Docker
2+
3+
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.
4+
5+
## How to use
6+
7+
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:
8+
9+
```bash
10+
npx create-next-app --example with-docker nextjs-docker
11+
# or
12+
yarn create next-app --example with-docker nextjs-docker
13+
```
14+
15+
## Using Docker
16+
17+
1. [Install Docker](https://docs.docker.com/get-docker/) on your machine.
18+
1. Build your container: `docker build -t nextjs-docker .`.
19+
1. Run your container: `docker run -p 3000:3000 nextjs-docker`.
20+
21+
You can view your images created with `docker images`.
22+
23+
### In existing projects
24+
25+
To add support for Docker to an existing project, just copy the `Dockerfile` into the root of the project and add the following to the `next.config.js` file:
26+
27+
```js
28+
// next.config.js
29+
module.exports = {
30+
// ... rest of the configuration.
31+
experimental: {
32+
outputStandalone: true,
33+
},
34+
}
35+
```
36+
37+
This will build the project as a standalone app inside the Docker image.
38+
39+
## Deploying to Google Cloud Run
40+
41+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) so you can use `gcloud` on the command line.
42+
1. Run `gcloud auth login` to log in to your account.
43+
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.
44+
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.
45+
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.
46+
47+
- You will be prompted for the service name: press Enter to accept the default name, `helloworld`.
48+
- 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`.
49+
- You will be prompted to **allow unauthenticated invocations**: respond `y`.
50+
51+
Or click the button below, authorize the script, and select the project and region when prompted:
52+
53+
[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run/?git_repo=https://github.com/vercel/next.js.git&dir=examples/with-docker)
54+
55+
## Running Locally
56+
57+
First, run the development server:
58+
59+
```bash
60+
npm run dev
61+
# or
62+
yarn dev
63+
```
64+
65+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
66+
67+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
68+
69+
[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`.
70+
71+
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.

app.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "nextjs",
3+
"options": {
4+
"allow-unauthenticated": true,
5+
"memory": "256Mi",
6+
"cpu": "1",
7+
"port": 3000,
8+
"http2": false
9+
}
10+
}

fly.toml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# fly.toml file generated for web3proxy on 2022-02-23T11:53:40-05:00
2+
3+
app = "web3proxy"
4+
5+
kill_signal = "SIGINT"
6+
kill_timeout = 5
7+
processes = []
8+
9+
[env]
10+
11+
[experimental]
12+
allowed_public_ports = []
13+
auto_rollback = true
14+
15+
[[services]]
16+
http_checks = []
17+
internal_port = 3000
18+
processes = ["app"]
19+
protocol = "tcp"
20+
script_checks = []
21+
22+
[services.concurrency]
23+
hard_limit = 25
24+
soft_limit = 20
25+
type = "connections"
26+
27+
[[services.ports]]
28+
handlers = ["http"]
29+
port = 80
30+
31+
[[services.ports]]
32+
handlers = ["tls", "http"]
33+
port = 443
34+
35+
[[services.tcp_checks]]
36+
grace_period = "1s"
37+
interval = "15s"
38+
restart_limit = 0
39+
timeout = "2s"

next.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
experimental: {
3+
outputStandalone: true,
4+
},
5+
}

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"dev": "next dev",
5+
"build": "next build"
6+
},
7+
"dependencies": {
8+
"next": "latest",
9+
"react": "^17.0.2",
10+
"react-dom": "^17.0.2",
11+
"next-http-proxy-middleware": "^1.2.1"
12+
}
13+
}

pages/_app.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../styles/globals.css'
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp

pages/api/web3/[...all].js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import httpProxyMiddleware from "next-http-proxy-middleware";
2+
3+
export default (req, res) => httpProxyMiddleware(req, res, {
4+
// You can use the `http-proxy` option
5+
target:'https://api.web3.storage/',
6+
headers: {"authorization": `Bearer ${process.env.WEB_3_STORAGE_TOKEN}`},
7+
// In addition, you can use the `pathRewrite` option provided by `next-http-proxy`
8+
pathRewrite: [{
9+
patternStr: '^/api/web3',
10+
replaceStr: ''
11+
}],
12+
});
13+
14+
export const config = {
15+
api: {
16+
bodyParser: false,
17+
externalResolver: true,
18+
}
19+
};

pages/index.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Head from 'next/head'
2+
import styles from '../styles/Home.module.css'
3+
4+
export default function Home() {
5+
return (
6+
<div className={styles.container}>
7+
<Head>
8+
<title>Create Next App</title>
9+
<link rel="icon" href="/favicon.ico" />
10+
</Head>
11+
12+
<main className={styles.main}>
13+
<h1 className={styles.title}>
14+
Welcome to <a href="https://nextjs.org">Next.js</a> on Docker!
15+
</h1>
16+
17+
<p className={styles.description}>
18+
Get started by editing{' '}
19+
<code className={styles.code}>pages/index.js</code>
20+
</p>
21+
22+
<div className={styles.grid}>
23+
<a href="https://nextjs.org/docs" className={styles.card}>
24+
<h3>Documentation &rarr;</h3>
25+
<p>Find in-depth information about Next.js features and API.</p>
26+
</a>
27+
28+
<a href="https://nextjs.org/learn" className={styles.card}>
29+
<h3>Learn &rarr;</h3>
30+
<p>Learn about Next.js in an interactive course with quizzes!</p>
31+
</a>
32+
33+
<a
34+
href="https://github.com/vercel/next.js/tree/canary/examples"
35+
className={styles.card}
36+
>
37+
<h3>Examples &rarr;</h3>
38+
<p>Discover and deploy boilerplate example Next.js projects.</p>
39+
</a>
40+
41+
<a
42+
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
43+
className={styles.card}
44+
>
45+
<h3>Deploy &rarr;</h3>
46+
<p>
47+
Instantly deploy your Next.js site to a public URL with Vercel.
48+
</p>
49+
</a>
50+
</div>
51+
</main>
52+
53+
<footer className={styles.footer}>
54+
<a
55+
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
56+
target="_blank"
57+
rel="noopener noreferrer"
58+
>
59+
Powered by{' '}
60+
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
61+
</a>
62+
</footer>
63+
</div>
64+
)
65+
}

0 commit comments

Comments
 (0)