Nuxt.js: how to use docker?

Created on 28 Jul 2017  ·  17Comments  ·  Source: nuxt/nuxt.js

Dockerfile

FROM node:6.10.2
ENV HOST 0.0.0.0

RUN mkdir -p /app

EXPOSE 3000

COPY . /app
WORKDIR /app
RUN npm install
RUN npm run build

CMD [ "npm", "start" ]

npm install

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm info ok
---> 1242ffb8765c
Removing intermediate container 26306d862022

npm build happen error

npm info lifecycle [email protected]~build: Failed to exec build script
npm ERR! Linux 3.13.0-100-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build"
npm ERR! node v6.10.2
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] build: `nuxt build && backpack build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script 'nuxt build && backpack build'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the spr package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! nuxt build && backpack build
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs spr
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls spr
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /app/npm-debug.log
Removing intermediate container 2e1d415c77af
The command '/bin/sh -c npm run build' returned a non-zero code: 1
help-wanted

Most helpful comment

@wsdo For your problem I think you are setting NODE_ENV=production (or already set by base image) just before build STEP and Eslint is always enabled on nuxt.config.js....

Simple but not best solution is changing build step to : RUN NODE_ENV=build npm install

Also as a side note, we are internally working to investigate best practices for high quality and production ready docker/swarm based Nuxt.js deployments. Stay tuned :)

All 17 comments

I was struggling with docker yesterday and this is my working settings 👍 hope this helps

Dockerfile

FROM node:8.2.1

ENV NODE_ENV=production
ENV HOST 0.0.0.0

RUN mkdir -p /app
COPY . /app
WORKDIR /app
# Expose the app port
EXPOSE 3000

RUN npm install
RUN npm run build
CMD ["npm", "start"]

.dockerignore // Since you copy the whole folder, including node_modules could leading to problems.

.nuxt

i try
result
@wushan

npm install error
image
image

How do you build?

You can basically ignore the fsevents warning but that eslint-loader isn't seems right, can you build the project without docker successfully ?

my packpage.json There are "eslint-loader": "^1.6.1", Don't know why no success

{
  "name": "nuxt_base",
  "version": "1.0.0",
  "description": "Nuxt.js project",
  "author": "wangshudong <[email protected]>",
  "private": true,
  "dependencies": {
    "nuxt": "latest"
  },
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate",
    "lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
    "precommit": "npm run lint"
  },
  "devDependencies": {
    "babel-eslint": "^7.1.1",
    "eslint": "^3.15.0",
    "eslint-config-standard": "^6.2.1",
    "eslint-loader": "^1.6.1",
    "eslint-plugin-html": "^2.0.0",
    "eslint-plugin-promise": "^3.4.1",
    "eslint-plugin-standard": "^2.0.1"
  }
}

do you use qq or wechat? @wushan

Try wrap your app in express for production might solve the issue, and no, I don't have qq nor wechat.express-template

@wsdo For your problem I think you are setting NODE_ENV=production (or already set by base image) just before build STEP and Eslint is always enabled on nuxt.config.js....

Simple but not best solution is changing build step to : RUN NODE_ENV=build npm install

Also as a side note, we are internally working to investigate best practices for high quality and production ready docker/swarm based Nuxt.js deployments. Stay tuned :)

@pi0 Any news/link on the docker/swarm based Nuxt.js deployments?

@pi0, I'm also interested in running Nuxt with Docker. Any update on this?

@pi0 any update?

@pi0 hey there, any updates?

Same error for me, any update for this issue? :(

After a long time of testing I finally was able to use docker with Nuxt

Dockerfile

FROM node:8.9.1

RUN mkdir -p /app
COPY . /app
WORKDIR /app

COPY package.json /app
COPY package-lock.json /app
RUN npm install

ENV NODE_ENV=production

COPY . /app
RUN npm run build

ENV HOST 0.0.0.0
EXPOSE 3000
CMD ["npm", "start"]

then I run:
sudo docker build -t <image-name> .

fater build is done:
sudo docker run -it -p 3000:3000 <image-name>

After that I was abel to successfully go to localhost port 3000 :)

I hope this helps

@besnikh Your Dockerfile worked for me. My biggest misunderstanding was the usage of ENV HOST 0.0.0.0 and how that was different from just letting my server run on localhost within the container by default.

Here is another version of Dockerfile that appears to work for me:
(I'm not too sure if me not changing WORKDIR will cause trouble for me later)

FROM node:10.7

LABEL Author Chris Dillinger

COPY ./package*.json ./
RUN npm install

ENV NODE_ENV=production

COPY . .
RUN npm run build

ENV HOST 0.0.0.0
EXPOSE 3000
CMD ["npm", "start"]

My .dockerignore file looks like:

npm-debug.log
.nuxt

I build with:
docker build -f Dockerfile -t <name>:<tag> .

I run with:
docker run -d -p 3000:3000 <name>:<tag>

@ChrisDillinger Happy that it helped.

I am using the following on production server with kubernetes

FROM node:10.9.0-alpine

RUN mkdir -p /app
COPY . /app
WORKDIR /app

RUN apk update && apk upgrade && apk add git

COPY package.json /app
#RUN npm rebuild
RUN npm install

ENV NODE_ENV=production

COPY . /app
RUN npm run build

ENV HOST 0.0.0.0
EXPOSE 3000

CMD ["npm", "start"]

you can notice #RUN npm rebuild that is to be run first time after changing node version than after that you do not need to rebuild.

Cheeers

Have you guys managed to run it in a local environment with hot reload from Docker?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  ·  3Comments

gary149 picture gary149  ·  3Comments

shyamchandranmec picture shyamchandranmec  ·  3Comments

bimohxh picture bimohxh  ·  3Comments

nassimbenkirane picture nassimbenkirane  ·  3Comments