This is my code. It's a hello world app with Next 9.0.1
Does not work in docker.
docker build -t cgcweb .
Next 9 does not work in Docker.
Here is my repo: https://github.com/WillowHQ/dockerIssue
yarn
yarn run build
yarn run start
Works as expected.
docker build -t cgcweb .
Sending build context to Docker daemon 1.076MB
Step 1/9 : FROM node:8.9.4
---> 672002a50a0b
Step 2/9 : COPY . .
---> 930ba71436a7
Step 3/9 : RUN rm -rf node_modules
---> Running in 6a02f97d7e04
Removing intermediate container 6a02f97d7e04
---> 417bb64d94c6
Step 4/9 : RUN rm -rf .next
---> Running in fb08b231ad4c
Removing intermediate container fb08b231ad4c
---> 21565ed63d03
Step 5/9 : RUN rm yarn.lock
---> Running in 262dd1b4f4a6
Removing intermediate container 262dd1b4f4a6
---> d4397a346e97
Step 6/9 : RUN yarn install
---> Running in b7432590fedd
yarn install v1.3.2
warning package.json: No license field
info No lockfile found.
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 14.75s.
Removing intermediate container b7432590fedd
---> 1f5bc4dd889f
Step 7/9 : RUN yarn run build
---> Running in 4df33457b0f6
yarn run v1.3.2
warning package.json: No license field
$ next build
Build error occurred
{ Error: ENOENT: no such file or directory, stat '/dev/fd/10' errno: -2, code: 'ENOENT', syscall: 'stat', path: '/dev/fd/10' }
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The command '/bin/sh -c yarn run build' returned a non-zero code: 1
- Click on '....'
- Scroll down to '....'
- See error
docker run -p 3000:3000 -it cgcweb
A clear and concise description of what you expected to happen.
I'd expect Next to work in Docker.
If applicable, add screenshots to help explain your problem.
Add any other context about the problem here.
Duplicate of #7934 ?
Investigating.
Well that updated docker file does not work either. I still get this error message:
$ next start
{ SyntaxError: Expected identifier, string or number
at Script (vm.js:87:7)
at createScript (vm.js:251:3)
at runInThisContext (vm.js:303:3)
at Module.prototype._compile (internal/modules/cjs/loader.js:656:3)
at Module._extensions[.js] (internal/modules/cjs/loader.js:699:3)
at Module.prototype.load (internal/modules/cjs/loader.js:598:3)
at tryModuleLoad (internal/modules/cjs/loader.js:537:5)
at Module._load (internal/modules/cjs/loader.js:529:3)
at Module.prototype.require (internal/modules/cjs/loader.js:636:3)
at require (internal/modules/cjs/helpers.js:20:7)
description: 'Expected identifier, string or number',
message: 'Expected identifier, string or number',
line: 27,
column: 38,
length: 3,
source:
'\t\tconst foundPath = await runMatcher({...options, cwd: directory});',
url: '/node_modules/find-up/index.js' }
Using that updated Dockerfile I can build but I still get the error on running the container.
I'm experiencing something very similar to OP except that I can get my docker image to build on my laptop, but when I attempt to deploy via Jenkins / Kubernetes, the missing directory message I get is:
{ Error: ENOENT: no such file or directory, stat '/.micro/static'
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: '/.micro/static' }
I had similar issues but I think copying contents to separate directory and running yarn from there fixes problem.
FROM node:8.9.4
- COPY . .
- RUN rm -rf node_modules
- RUN rm -rf .next
- RUN rm yarn.lock
+ COPY . /app
+ WORKDIR /app
RUN yarn install
RUN yarn run build
EXPOSE 3000
CMD [ "yarn", "run", "start" ]
I also added .dockerignore with:
.next/
node_modules/
Dockerfile
Updating the Dockerfile fixes the issue.
Closing because OP resolved per https://github.com/zeit/next.js/issues/7938#issuecomment-511498518.
@masives Why does putting the files into a separate directory fix it ?
@WillowHQ Honestly I'm not sure why it works, just thought spreading application contents on root level as an antipattern.
In my opinion you should separate your application from system it's running on - same you don't put your project contents in windows/system32 directory.
I believe it has to do with permissions on root level (where you couldn't create these files) or by copying a directory you automatically become it's owner thus allowing you to make changes there.
These are just speculations unfortunately.
I'm experiencing something very similar to OP except that I can get my docker image to build on my laptop, but when I attempt to deploy via Jenkins / Kubernetes, the missing directory message I get is:
{ Error: ENOENT: no such file or directory, stat '/.micro/static' errno: -2, code: 'ENOENT', syscall: 'stat', path: '/.micro/static' }
Did you manage to solve this issue?
Many thanks to @masives as his suggestion was part of the solution for us. In our case, we needed to filter out protected files / directories created in our pod with a .dockerignore so when we copied our app files to a new directory the protected files / directories were not present to interfere with the build. We also needed to bump the resource allocation to the pod (not by a lot) as the build was crashing due to insufficient ram / cpu configurations.
I've also got it fixed by the suggestion of @masives. Excluding the files / firectories with an .dockerignore fixed the issue.
Most helpful comment
Many thanks to @masives as his suggestion was part of the solution for us. In our case, we needed to filter out protected files / directories created in our pod with a
.dockerignoreso when we copied our app files to a new directory the protected files / directories were not present to interfere with the build. We also needed to bump the resource allocation to the pod (not by a lot) as the build was crashing due to insufficient ram / cpu configurations.