Home > Mobile >  Nuxt build fails with permission error inside docker container
Nuxt build fails with permission error inside docker container

Time:03-09

I'm trying to dockerize my Nuxt app by following this guide. It works if I enter the commands written in there, but I want to simplify it using docker-compose. I'm still learning about docker.

So far, I have created a docker-compose.yml file

version: '3'

services:
  nuxt-app:
    container_name: nuxt-app
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - '3000:3000'

But when I run docker-compose up, it shows me this error

[ ] Running 2/2
⠿ Network nuxt-app_default  Created 0.7s
⠿ Container nuxt-app        Created 30.3s

Attaching to nuxt-app
nuxt-app  | yarn run v1.22.17
nuxt-app  | $ nuxt start
nuxt-app  |
nuxt-app  |  FATAL  No build files found in /app/.nuxt/dist/server.
nuxt-app  | Use either `nuxt build` or `builder.build()` or start nuxt in development mode.
nuxt-app  |
nuxt-app  |   Use either `nuxt build` or `builder.build()` or start nuxt in development mode.
nuxt-app  |   at VueRenderer._ready (node_modules/@nuxt/vue-renderer/dist/vue-renderer.js:758:13)
nuxt-app  |   at async Server.ready (node_modules/@nuxt/server/dist/server.js:637:5)
nuxt-app  |   at async Nuxt._init (node_modules/@nuxt/core/dist/core.js:482:7)
nuxt-app  |
nuxt-app  |
nuxt-app  |
╭──────────────────────────────────────────────────────────────────────────────╮
│                                                                              │
│   ✖ Nuxt Fatal Error                                                         │
│                                                                              │
│   Error: No build files found in /app/.nuxt/dist/server.                     │
│   Use either `nuxt build` or `builder.build()` or start nuxt in              │
│   development mode.                                                          │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
nuxt-app  |
nuxt-app  | error Command failed with exit code 1.
nuxt-app  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
nuxt-app exited with code 1

My Dockerfile look like this

FROM node:lts as builder

WORKDIR /app

COPY . .

RUN yarn install \
  --prefer-offline \
  --frozen-lockfile \
  --non-interactive \
  --production=false

RUN yarn build

RUN rm -rf node_modules && \
  NODE_ENV=production yarn install \
  --prefer-offline \
  --pure-lockfile \
  --non-interactive \
  --production=true

FROM node:lts

WORKDIR /app

COPY --from=builder /app  .

ENV HOST 0.0.0.0
EXPOSE 80

CMD [ "yarn", "start" ]

Would somebody please let me know why is this happening and how can I make it work?

UPDATE

The issue seems to be resolved if I do yarn build before docker-compose up

CodePudding user response:

The volumes: mount is hiding everything your Dockerfile does. After building the application and creating a minimal image around it, you then mount your host directory and make all of that invisible. That's also why running yarn build locally seems to help: it recreates the directory your Dockerfile would have built on your host before mounting it into the container.

You can reduce the docker-compose.yml to just

version: '3.8'
services:
  nuxt-app:
    build: .
    ports:
      - '3000:3000'

without volumes: and without overriding the container_name: Compose provides on its own, and using the shorter form of build: since you're otherwise using default options.

  • Related