FROM node:18-alpine WORKDIR /app COPY package.json package-lock.json ./ RUN npm install COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "start"]
This Dockerfile installs dependencies, builds the Next.js app, and then runs it in production mode using npm start. The app listens on port 3000 as exposed.
The npm install --only=production flag installs only production dependencies. Other options are either invalid or deprecated.
FROM node:18-alpine WORKDIR /app COPY package.json package-lock.json ./ RUN npm install COPY . . RUN npm run build EXPOSE 3000 CMD ["node", "server.js"]
Next.js apps by default do not have a server.js file unless custom server code is added. Running node server.js causes the app not to start properly, resulting in 404 errors.
Multi-stage builds let you separate build dependencies from the final image, keeping only what is needed to run the app. This reduces image size and attack surface.
Setting environment variables in the Dockerfile with ENV makes them available at build and runtime. Next.js accesses them via process.env. This is reliable and clear.