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
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 runningdocker-compose build
, thenode_modules
and.next
folders are created inside container, but after runningdocker-compose up -d
, volumes fromdocker-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.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:If there are errors with this Dockerfile, you need to remove the node container (
docker stop <container_name>
anddocker rm <container_name>
), then rundocker-compose build
anddocker-compose up -d
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.