Commit graph

37 commits

Author SHA1 Message Date
Koji Onishi
430e71a38d
update Docker examples to specify HOSTNAME properly (#59756)
### What?

Update docker examples to correctly set HOSTNAME env variable for
standalone output

Related to PR:
https://github.com/vercel/next.js/pull/52804

### Why?
### TL;DR

The dockerfiile examples need to modified as that contains HOSTNAME
setting that apparently is not working as expected.

Related to Issue:
https://github.com/vercel/next.js/issues/58657

Affected examples:
https://github.com/vercel/next.js/tree/canary/examples/with-docker

https://github.com/vercel/next.js/tree/canary/examples/with-docker-multi-env

### Longer explanations

Based on [Docker
document](https://docs.docker.com/engine/reference/builder/#environment-replacement):
> You can also use environment variables with RUN, CMD, and ENTRYPOINT
instructions, but in those cases the variable substitution is handled by
the command shell, not the builder.

It means that when executing the last CMD ( `node server.js` ) in
Dockerfile samples, the HOSTNAME defined by prior `ENV` instruction is
simply ignored.

This causes problems typically when the host process sets HOSTNAME -
e.g. for instance when using AWS Fargate for deployment [we can't have
control over HOSTNAME](https://stackoverflow.com/a/52871552) set by the
host process. (also refer to the issue #58657 above)

### How?

Updated Dockerfilie samples, by setting HOSTNAME directly in CMD
instruction that launches nextjs server, not in the builder process.

Used this setting at my end (with AWS Fargate) to confirm that it fixes
the network problem.

Closes NEXT-
Fixes #58657

Co-authored-by: Sam Ko <sam@vercel.com>
2024-03-19 21:26:44 +00:00
Sam Ko
3f1ae54319
Revert "fix: permissions in with-docker example" (#63165)
Reverts vercel/next.js#63134

Closes NEXT-2783
2024-03-11 17:36:11 -07:00
Kinbaum
51665b3640
fix: permissions in with-docker example (#63134)
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

### What

This pr addresses an issue in Docker where the cache and the creation of
its contents throw the following error:

```
[Error: EACCES: permission denied, mkdir '/app/.next/cache'] {
errno: -13,
code: 'EACCES',
syscall: 'mkdir',
path: '/app/.next/cache'
```

Co-authored-by: Sam Ko <sam@vercel.com>
2024-03-11 16:33:03 +00:00
JA
b654effd53
fix(with-docker): resolve build with pnpm (#61529)
### Adding or Updating Examples

- Example:
[with-docker](https://github.com/vercel/next.js/tree/canary/examples/with-docker)
- Update: The latest change introduced in #61466 on the building process
of NextJS when the package manager is PNPM is currently throwing an
error (it works for npm though).

**To replicate the error:**

1. Checkout on the **with-docker** example
2. Install with PNPM so that it generates the pnpm-lock.yaml
3. Build the Docker image -> This will fail on the _builder stage_ step
that runs `pnpm run build`.

**Possible Solution:**
- Run `corepack enable pnpm` before building with pnpm
2024-02-01 19:32:57 +00:00
Stefan Generalao
e0c63aca99
fix: dockerfile npm run build (#61466) 2024-01-31 18:46:33 +00:00
Sam Ko
8d13ab925e
chore(examples): switch default from yarn to npm in Dockerfiles (#61348)
## Changes

- Switch default package manager to npm
- Fixes https://github.com/vercel/next.js/issues/61293

Closes NEXT-2291
2024-01-29 14:38:31 -08:00
ihommani
70389d0cfd
examples: update Dockerfile comment (#56646)
Co-authored-by: Lee Robinson <me@leerob.io>
2023-11-15 23:11:21 -06:00
Rui Ying
49a891deb5
Fix the prerender cache update error with the example Dockerfile (#54870)
It seems the app router attempts to write its cache to both `.next/cache` and `.next/server` in docker.

This fix should fix standalone output server's error `Failed to update prerender cache... [Error: EACCES: permission denied...` when deployed in a docker container with the example Dockerfile.

Related discussion: https://github.com/vercel/next.js/discussions/51164
2023-09-01 17:19:13 +00:00
Mohamed A. Salah
b7eb6d43b9
set hostname to 0.0.0.0 (#54342)
When I try to deploy to Google Cloud Run it fails after some
investigation I saw this line in

.next/standalone/server.js
```javascript
const hostname = process.env.HOSTNAME || 'localhost'
``` 
This some how make this log when i run docker 
```shell
- ready started server on 172.17.0.2:3000, url: http://172.17.0.2:3000
``` 
I don't know why it's logging this address even if the server running on
localhost.

So this my fix
Set hostname to 0.0.0.0 to avoid deployment failing on Google cloud run.
2023-08-21 19:05:57 +02:00
Tim Neutkens
d60690e2bd
Update Docker example to remove HOSTNAME (#54102)
With the latest changes in stable Next.js setting HOSTNAME is not longer required for the image to function correctly 👍 

Fixes #54093
2023-08-16 10:08:38 +00:00
Tim Neutkens
c9fef184fd
Revert "Move USER and remove redundant --chown from Dockerfile (#53441)" (#54046)
This reverts commit 39c06ae95e.

Based on
https://github.com/vercel/next.js/pull/53441#pullrequestreview-1578047015

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
2023-08-15 10:37:17 +02:00
Stef
39c06ae95e
Move USER and remove redundant --chown from Dockerfile (#53441)
### What?

In the Dockerfile example:

* Moves the `USER` command above the `COPY`s
* Removes the `--chown` on the `COPY`s 

> **Note**
> I don't know for 100% sure this won't have unintended side effects. Part of my motivation for opening this PR is to sense check whether we will be causing a regression by making this change in our own projects.
> 
> Please let me know if there's any concerns with this!

### Why?

Sonar security scanner flags this Dockerfile as having "security hotspots" due to the use of `--chown`

https://rules.sonarsource.com/docker/RSPEC-6504/

### How?

Make the `--chown`s redundant by setting the current user before doing the `COPY` commands.
2023-08-03 23:36:08 +00:00
Balázs Orbán
10f74a1d45
Revert "docker examples: improve perf by using COPY --link" (#53440)
Reverts vercel/next.js#52835 because it does not work on Google Cloud Run, which we mention in the README.md.

Closes #53425
2023-08-01 12:48:57 +00:00
Kévin Dunglas
d8660bb059
docker examples: improve perf by using COPY --link (#52835)
### What?

Uses `COPY --link` instructions in Docker examples.

### Why?

This significantly [accelerates Docker builds and improves caching](https://www.howtogeek.com/devops/how-to-accelerate-docker-builds-and-optimize-caching-with-copy-link/).

This instruction is supported for more than 1 year and is now supported by almost all modern Docker installations.
We use it successfully for a while in [the API Platform distribution](https://github.com/api-platform/api-platform/blob/main/pwa/Dockerfile), which includes Next.js and is derived from these examples.

### How?

By updating the existing examples.
2023-07-18 20:44:46 +00:00
Eldar Amantay
0084166caa
chore(docs): Add mentioning of HOSTNAME env variable for standalone output (#52804)
### What?

Update documentation to mention a `HOSTNAME` env variable for `standalone` output

Related to Feature: https://github.com/vercel/next.js/pull/44627 

### Why?

Current documentation mentions only `PORT` env, but does not mention `HOSTNAME` added in January

Affected documentation pages:

- https://nextjs.org/docs/app/api-reference/next-config-js/output

Affected examples:

- https://github.com/vercel/next.js/tree/canary/examples/with-docker
- https://github.com/vercel/next.js/tree/canary/examples/with-docker-multi-env

### How?

- Run examples
- Check documentation
2023-07-17 19:59:53 +00:00
Bennett Dams
7fde79a9e8
Define base image for multi-stage build (#45090)
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-02-07 15:21:20 -08:00
Luke Schlangen
836ac9cc7f
bump node alpine to version 18 (LTS) (#45643)
Updating node version to LTS https://nodejs.org/
I tested locally and deploying to Google Cloud Run and build and deploy steps were successful in both cases.

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-07 02:31:38 +00:00
Crazy Urus
d74f34021b
Add --frozen-lockfile in Docker example when using pnpm (#41291)
It seems that lock file is used when using npm and yarn, but pnpm doesn't, complete this code.

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-10 12:10:14 +00:00
Jonas Strassel
638e6d16f8
chore(with-docker): don't copy package.json twice (#39896)
the directory `/app/.next/standalone` already contains a copy of the package.json so we can skip copying it before we copy the standalone dir

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-08-24 09:35:44 -05:00
Duc Tran
b7997105a0
fix: use if...else in dockerfile (#39263)
* fix: use if-else in dockerfile

* fix: remove typo
2022-08-02 17:01:24 -05:00
Balázs Orbán
0a781dd675
fix: use if..elif..else in docker examples (#39072)
* fix: use `if..elif..else` in docker examples

* fix missing devDependencies for build

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-07-27 09:54:04 -05:00
Balázs Orbán
35a3243854
chore: auto-detect preferred package manager in Dockerfile (#38693) 2022-07-18 13:45:39 -05:00
Chris Midgley
946f465a6e
fix(examples/with-docker): add npm comment for yarn build (#35040)
There was an `npm` equivalent for `yarn install` (`npm ci`) but not one for `yarn build`. I've added `npm run build` as a commented out option.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-03-04 17:59:57 +00:00
Ekaterina Anishkina
07d9c92124
fix(examples/with-docker): update env comments (#29972)
This fixes the comment about disabling telemetry. For now it doesn't disable telemetry for `next build` if you uncomment it.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## 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
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
2022-02-05 21:41:25 +00:00
Janne Sinivirta
97b5c0ae5e
Fix ambiguous flags in Dockerfile example (#33417)
* Fix ambiguous flags in Dockerfile example

At least with Docker xx this Dockerfile fails to both `adduser` and `addgroup` commands due to ambiguous flags. The error message for example for `addgroup` is:

```
#5 0.292 Option g is ambiguous (gecos, gid, group)
#5 0.292 Option s is ambiguous (shell, system)
```
This PR switches both commands to use the long-format flags. I think they are also more understandable for the readers of the Dockerfile.

* Apply suggestions from code review

Co-authored-by: Balázs Orbán <info@balazsorban.com>

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-02-05 15:26:41 -06:00
JJ Kasper
d005631063
Update npm comment in Docker example (#33881) 2022-02-01 16:52:26 -06:00
Kryštof Krátký
33784f1342
Fix with-docker example dockerfile (#33695)
Fixed `dockerfile` in `with-docker`  example

## Bug

- error when executing `docker build -t nextjs-docker .` - `yarn.lock` file is missing

 ## Documentation / Examples

- edited to not fail when `package-lock.json` or `yarn.lock` are not found
- example: use `with-docker` example

- [x] Make sure the linting passes by running yarn lint

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-01-27 17:30:52 +00:00
JJ Kasper
c320249be1
Remove un-necessary second yarn install from example Dockerfile (#32934)
Update to remove the second `yarn install` step from the docker example as it is no longer needed since we only include necessary files from the output file traces now. 

Closes: https://github.com/vercel/next.js/pull/29360

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-01-01 20:49:43 +00:00
Marcelo Carmona
c87204eb77
Update 14-alpine to 16-alpine (#31777)
## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2021-12-16 06:35:25 +00:00
JJ Kasper
ca8fd316f1
Update docker image to leverage output traces (#32258)
This updates our docker example to leverage the output traces and standalone build to reduce the resulting docker image quite a bit. 

docker image size before: `272MB`
docker image size after: `121MB`
node-14:alpine size (base image): `118MB`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

x-ref: https://github.com/vercel/next.js/pull/32255
x-ref: https://github.com/vercel/next.js/issues/32252
x-ref: https://github.com/vercel/next.js/issues/30822
2021-12-13 13:11:12 +00:00
Aryan Beezadhur
e63322f432
Update Dockerfile (#32299)
Update `Dockerfile` to match [Next.js docs](https://nextjs.org/docs/deployment#docker-image).
2021-12-12 20:52:34 +00:00
stef
5abfea3765
Recommend using next CLI for Docker entrypoint and not yarn (#29024)
Fixes #29023 

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

## 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
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes
2021-11-01 20:26:14 +00:00
LiuuY
a38167f24b
Fix with-docker example fails to load image (#25536)
node version should be 14, otherwise, next/image fails to load static file images located in public dir
detail: https://stackoverflow.com/questions/67641673/next-js-app-running-inside-a-docker-container-fails-to-load-static-file-images
2021-05-28 11:56:12 -05:00
Martin Janeček
1f54ba6559
docs: reduce image size in sample Dockerfile (#24108) 2021-04-19 22:00:56 -05:00
Lee Robinson
d49f978a17
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
2021-03-28 20:32:09 +00:00
Nghiệp
111d2df90c
[Examples] Optimize with-docker (#17116)
* Fix missing yarn.lock
* Add --frozen-lockfile flag to speed up install deps
* Make sure node_modules do not contains devDependencies
* Add --targe stage docker build
2020-09-21 04:09:37 +00:00
Michael Hsu
738376c812 Add docker example (#4261)
* Add docker example

* fix lint
2018-05-05 04:12:56 -07:00