25

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.

asked Apr 5, 2018 at 15:37
6
  • Did yout try adding the next build src bit to the prod command? Commented Apr 10, 2018 at 21:26
  • Why using nodemon and not the next start command? Commented Oct 14, 2018 at 17:23
  • have you solved this issue yet? My docker-compose does not build .next in docker file Commented Dec 17, 2018 at 4:57
  • Try changing the production directory from .next to something without a starting period, perhaps _next. Commented Sep 4, 2019 at 11:28
  • Why do you have to copy & install node_modules in /tmp, and copy back to WORKDIR? Usually, WORKDIR automatically creates the directory while running dockerfile command. And also, did you check the message while building the docker image, is there any abnormal message? Commented Oct 26, 2019 at 1:54

5 Answers 5

2

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

answered Apr 14, 2018 at 7:04
Sign up to request clarification or add additional context in comments.

2 Comments

It is done already. It was never uploaded to the docker environment from the beginning.
Then you need provide a minimal repo to reproduce the issue
2

As this issue has gotten quite a few extra votes over time, let me elaborate a bit on a few things you might want to take into account when facing the above problem.

Version

Please refer to the official docs where Vercel lists Node.js version 10.13 or later as a requirement.

Using hardened images for production

I would also recommend using images that are production hardened and proven to be secure. It's considered good practice to use open source images like those of Bitnami (why?, example). Note that this will solve your problem, as now you no longer use any of your local files.

Guarantee build integrity

Recommend ignoring node_modules along with any build artifacts like .next in your .dockerignore file and mounting the cache folder for yarn or npm during build time. That way you can be sure to generate node_modules with the correct OS bindings for your image.

answered May 27, 2020 at 21:26

Comments

0

Check your .dockerignore

FROM node:latest
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install pm2 -g
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
# Show current folder structure in logs
RUN ls -al -R
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
answered Jun 24, 2020 at 10:49

Comments

0

For production using nextjs use the below commands

npm run build && npm run start
answered Jun 11, 2021 at 14:36

Comments

0

Update from 2022

Please check the official example from vercel how to use nextjs and docker: https://github.com/vercel/next.js/tree/canary/examples/with-docker

They don't use npm run start, instead node server.js

Don't just copy Dockerfile... Look in next.config for standalone parametr, and package.json for nextjs version.

In next js 12.1.6 next.config should be:

module.exports = {
 eslint: {
 ignoreDuringBuilds: true,
 },
 experimental: {
 outputStandalone: true
 }
}
answered Aug 7, 2022 at 14:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.