Next.js: Do you guys have a dockerfile for deploying next.js?

Created on 28 Oct 2016  路  16Comments  路  Source: vercel/next.js

I have my dokku cluster and would love to deploy it using docker and not buildpacks.

Most helpful comment

Using this Dockerfile -- starts at ~130MB.

FROM node:12-alpine as build

COPY . /src
WORKDIR /src

RUN npm ci
RUN npm run build
RUN npm prune --production

FROM node:12-alpine

WORKDIR /usr/app

COPY --from=build /src/node_modules /usr/app/node_modules
COPY --from=build /src/package.json /usr/app/package.json
COPY --from=build /src/.next /usr/app/.next

ENV NODE_ENV production

CMD ["npm", "start"]

All 16 comments

Overall, I'd say you guys've beat create-react-app by a lot!

FROM node:6-onbuild
# Create app directory
RUN mkdir -p /usr/src/app 
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
RUN npm run build
EXPOSE 80

As I said I am trying to run this thing on my dokku, and exposing 80 kinda did the trick but now I can not get letsencrypt to work with this new app. Will keep investigating...

@ojosdegris Thanks for the research. It's really important to be able to deploy this everywhere. In the meanwhile, you can use now.

$ cd your-project
$ now

馃挜

I've made slight progress on figuring this out, but hit a roadblock.

.dockerignore:

npm-debug.log
.next

Dockerfile:

FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
RUN npm run build

# Bundle app source
COPY . /usr/src/app

EXPOSE 3000
CMD [ "npm", "start" ]

bash:

$ docker build -t my-nodejs-app .
$ docker run -it -P  --rm --name my-running-app my-nodejs-app
$ open http://localhost:32770/ # port number found using kitematic ui

Finally, I get a 404:
image

Ah, I just switched to node:6-onbuild instead of node:argon and everything worked fine 馃帀

Maybe we can add this to the wiki and close this issue?

I used the Dockerfile to build an image. The image size is 776 MB, is it too big? node:6-onbuild image is already 651 MB. Is there any slimmer node image that works?

@johannchen try node:alpine

Should work out of the box with any Node docker image. Next doesn't require anything special on top, other than running npm run build and npm run start.

Dockerfile mentioned here dont work for me `

Couldn't find a pages directory. Please create one under the project root`

I posted this tutorial which has a node-alpine image

https://medium.com/@sbr464/next-js-tutorial-deploy-to-docker-on-google-cloud-container-engine-6b0c19dd8ecb

This is what works for me. The issue with Couldn't find a pages directory. Please create one under the project root was caused by the postinstall script that tried to run next build, but at a moment that the files were not copied in yet.

My Dockerfile:

FROM node:11-alpine

WORKDIR /workspace

COPY package.json yarn.lock /workspace/

RUN yarn install

COPY . .

RUN yarn build

CMD ["yarn", "start"]

Using this Dockerfile -- starts at ~130MB.

FROM node:12-alpine as build

COPY . /src
WORKDIR /src

RUN npm ci
RUN npm run build
RUN npm prune --production

FROM node:12-alpine

WORKDIR /usr/app

COPY --from=build /src/node_modules /usr/app/node_modules
COPY --from=build /src/package.json /usr/app/package.json
COPY --from=build /src/.next /usr/app/.next

ENV NODE_ENV production

CMD ["npm", "start"]

@Meemaw Thank you very much

Below is my slightly production ready modified version

FROM node:13.7.0-alpine3.11 as build

COPY . .

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

RUN npm run build

FROM node:13.7.0-alpine3.11

COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build .next .next

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

EXPOSE 3000

CMD npm start

@whs-dot-hk Thank you.

Added public folder

FROM node:12-alpine as build

COPY . .

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

RUN npm run build

FROM node:12-alpine

COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build .next .next
COPY --from=build public public

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

EXPOSE 3000

CMD npm start

Here is my running application Dockerfile Create this inside the root folder -

FROM node:12

# Create app directory
RUN mkdir /var/movable/ && mkdir /var/movable/app
WORKDIR /var/movable/app

# Installing dependencies
COPY package*.json /var/movable/app/
RUN npm install

# Copying source files
COPY . /var/movable/app


# Building app
RUN npm run build

# Running the app
CMD "npm" "run" "start"

Thanks to @whs-dot-hk and @dickamin

Adding next.config.js (e.g. if you use basePath for routing)

FROM node:14-alpine as build

COPY . .

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

RUN npm run build

FROM node:14-alpine

COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build next.config.js next.config.js
COPY --from=build .next .next
COPY --from=build public public

ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production

EXPOSE 3000

CMD npm start
Was this page helpful?
0 / 5 - 0 ratings

Related issues

renatorib picture renatorib  路  3Comments

knipferrc picture knipferrc  路  3Comments

DvirSh picture DvirSh  路  3Comments

irrigator picture irrigator  路  3Comments

timneutkens picture timneutkens  路  3Comments