skip to Main Content

I have a VPS Ubuntu 22.04 and I want to build and run a Next.js application inside a Docker container.

It means that I don’t want NodeJS installed on Ubuntu, instead node:18-alpine should do all the work.

I’m facing an issue that after running the docker-compose build / docker-compose up -d commands, the container keeps restarting with the error:

[Error: ENOENT: no such file or directory, open '/app/.next/BUILD_ID'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/app/.next/BUILD_ID'
}

There are no node_modules and .next folders in the ./data/app directory on Ubuntu (since I want them to be created inside the container at the time of build and launch).

docker-compose.yml:

  node:
    build:
      context: ./data/app
      dockerfile: ./Dockerfile
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - ./data/app:/app:cached

Dockerfile:

FROM node:18-alpine

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install

RUN npm install -g next

COPY . .

RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]

package.json scripts:

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
},

2

Answers


  1. Chosen as BEST ANSWER

    As @DavidMaze mentioned, the problem is caused by mounting the volumes.

    Description of the cause of the problem:

    From the terms of the question, Ubuntu does not have NodeJS, so the project does not have node_modules and .next folders. After running docker-compose build, the node_modules and .next folders are created inside container, but after running docker-compose up -d, volumes from docker-comsope.yml are mounted, and since the host does not have these folders, they are mounted without them, so the container does not see theirs.

    How to fix the problem:

    You need to add the node_modules and .next folders as an exception so that they are not deleted in the container after mounting.

      node:
        build:
          context: ./data/app
          dockerfile: ./Dockerfile
        restart: unless-stopped
        ports:
          - "3000:3000"
        volumes:
          - ./data/app:/app
          - /app/node_modules <-- add this
          - /app/.next <-- add this
    

    After that, everything will work as it should.

    If you still have errors in your container due to the missing .next folder, then try this Dockerfile:

    FROM node:18-alpine
    
    RUN apk add --no-cache libc6-compat
    
    WORKDIR /app
    
    COPY . .
    
    RUN npm install
    
    RUN npm run build
    
    EXPOSE 3000
    
    CMD ["npm", "start"]
    

    If there are errors with this Dockerfile, you need to remove the node container (docker stop <container_name> and docker rm <container_name>), then run docker-compose build and docker-compose up -d


  2. There is a typo in the docker-compose.yml file’s container path where you want to mount the volume. Change /app:cached to /app/cached. That’s it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search