I have been tired of fixing this issue, I have been fixing this issue last two weeks but have not resolved it yet
docker image gets created successfully but when I run it then this gives the error
docker run -p 3000:3000 web-sphere:latest
> web-sphere@0.1.0 start
> next start
▲ Next.js 14.2.3
- Local: http://localhost:3000
✓ Starting...
Error: Could not find a production build in the '.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
at setupFsCheck (/app/node_modules/next/dist/server/lib/router-utils/filesystem.js:151:19)
at async initialize (/app/node_modules/next/dist/server/lib/router-server.js:61:23)
at async Server.<anonymous> (/app/node_modules/next/dist/server/lib/start-server.js:249:36)
I’m trying to run a Next.js application in a Docker container, but I’m getting an error stating that the production build could not be found in the ‘.next’ directory. I’ve followed the steps in the provided Dockerfile, but the issue persists.
here is my docker file:-
# Use the official Node.js 20 image as the base for the build stage
FROM node:20-alpine as builder
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Increase the timeout for npm
RUN npm config set fetch-retry-maxtimeout 60000
# Use a different npm registry (optional)
RUN npm config set registry https://registry.npmjs.org/
# Update npm to the latest version
RUN npm install -g npm@latest
# Install dependencies
RUN npm install
# Correctly copy the entire project
COPY . .
# Build the Next.js app
RUN npm run build
# After the npm run build step
RUN ls -la /app/.next
# Use the official Node.js 20 image as the base for the production stage
FROM node:20-alpine
# Set the working directory
WORKDIR /app
# Copy the entire app from the builder stage
COPY --from=builder /app .
# Install production dependencies
RUN npm install --production
# Expose the port the app will run on
EXPOSE 3000
# Start the app
CMD ["npm", "start"]
I’ve tried building my app with next build before starting the production server, but I still get this error. Has anyone else encountered this issue? Any help would be appreciated.
I merged the two topics you created. Please, don’t open two topics for the same question. I wanted to ask for the Dockerfile as well, then I noticed you shared it in the other topic.
Also please, format your posts according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.
Example code block:
```
echo "I am a code."
echo "An athletic one, and I wanna run."
```
I don’t have NodeJS experience, but it doesn’t seem to be a Docker issue. You can find similar issues on Google like this:
If you tried the exact same versions without Docker and you can tell us why you think this should work, we can try to help with the Docker part.
You can also run the container overriding the default command and start a shell to browse the folder and investigate why it is not a “production build”.