vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.36k stars 26.78k forks source link

Do you guys have a dockerfile for deploying next.js? #121

Closed viktor-evdokimov closed 7 years ago

viktor-evdokimov commented 7 years ago

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

viktor-evdokimov commented 7 years ago

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

viktor-evdokimov commented 7 years ago
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...

impronunciable commented 7 years ago

@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

💥

aesopwolf commented 7 years ago

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

.dockerignore:

node_modules
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

aesopwolf commented 7 years ago

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?

johannchen commented 7 years ago

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?

luisrudge commented 7 years ago

@johannchen try node:alpine

rauchg commented 7 years ago

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.

tokra commented 7 years ago

Dockerfile mentioned here dont work for me `

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

sbrichardson commented 7 years ago

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

beeman commented 5 years ago

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"]
Meemaw commented 4 years ago

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"]
whs-dot-hk commented 4 years ago

@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
dickamin commented 4 years ago

@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
desaijay315 commented 4 years ago

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"
toruta39 commented 3 years ago

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
malixsys commented 3 years ago

With pruning

FROM node:14-alpine as BUILD_IMAGE

WORKDIR /app

COPY package.json package-lock.json ./

# install dependencies
RUN npm install

COPY . .

# build
RUN npm run build

# remove dev dependencies
RUN npm prune --production

FROM node:14-alpine

WORKDIR /app

# copy from build image
COPY --from=BUILD_IMAGE /app/package.json ./package.json
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
COPY --from=BUILD_IMAGE /app/next.config.js next.config.js
COPY --from=BUILD_IMAGE /app/.next ./.next
COPY --from=BUILD_IMAGE /app/public ./public

EXPOSE 3000
CMD ["npm", "start"]
balazsorban44 commented 2 years ago

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.