Nextjs fails to find valid build in the '.next

2019-04-21 04:49发布

问题:

I am running my app in docker, but my production build and start script fails only in docker environment. Although node_env development works well in docker environment.

Here is my script that fails to make a production build and start a server. I am using nodemon and babel

"build:prod": {
      "command": "babel ./src/server/ -d server --presets es2015,stage-2 && next build src",
      "env": {
        "NODE_ENV": "production"
      }
    },
    "start:prod": {
      "command": "PORT=3000 nodemon --watch ./src/server/ ./src/server/server.js --exec babel-node --presets es2015,stage-2",
      "env": {
        "NODE_ENV": "production"
      }
    }

But when I give same command in docker environment:

FROM node:8-alpine

COPY package.json /tmp/package.json

RUN cd /tmp && npm install

RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app

WORKDIR /opt/app

ADD . /opt/app

RUN npm run build:prod

EXPOSE 3000

CMD ["npm", "run", "start:prod"]

I get the following error in docker:

 > better-npm-run start:prod
site_1      | 
site_1      | running better-npm-run in /opt/app
site_1      | Executing script: start:prod
site_1      | 
site_1      | to be executed: PORT=3000 NODE_ENV=production nodemon --watch ./src/server/ ./src/server/server.js --exec babel-node --presets es2015,stage-2 
site_1      | [nodemon] 1.17.3
site_1      | [nodemon] to restart at any time, enter `rs`
site_1      | [nodemon] watching: /opt/app/src/server/**/*
site_1      | [nodemon] starting `babel-node ./src/server/server.js --presets es2015,stage-2`
site_1      | false 'production'
site_1      | > Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.
site_1      | [nodemon] app crashed - waiting for file changes before starting...

I would appreciate any help and would be nice to know what I am doing wrong.

回答1:

You need to make sure the .next directory is not being copied from your host

ADD . /opt/app

Will also add the .next directory you had on host. I would add .dockerignore and add .next to the same. And then build and run again