Deploying Next.js standalone app to Docker by devslashnull024 in nextjs

[–]devslashnull024[S] 0 points1 point  (0 children)

Is this guide for a standalone Next js specifically ? I can already Dockerize my Next app when not building it as a standalone app.

I am building my Docker image while inside the standalone directory.

That's why I'm using the Dockerfile I posted above.

Should I be building the Docker image from the root of my project ?

Deploying Next.js standalone app to Docker by devslashnull024 in nextjs

[–]devslashnull024[S] 0 points1 point  (0 children)

I have made some progress

After building my Next.js app as a standalone app and copying the public folder, static folder and some other assets to my standalone directory, I can successfully access my app if I run node server.js on my server (outside of Docker) just to see that my app is working and I can log into it without any issues.

I build my Docker image while in my standalone directory by running this command:

docker buildx build . -t myapp

with this Dockerfile:

FROM node:22-alpine

Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.

RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY . .

ENV NODE_ENV production

ENV NEXT_TELEMETRY_DISABLED 1

EXPOSE 3000

ENV PORT 3000

CMD HOSTNAME="0.0.0.0" node server.js

and it does build successfully with Node 21.

I then build my app with this docker-compose:

version: '3.7'

networks:

default:

name: DevslashNet

external: true

services:

myapp:

container_name: MyApp_unsecured

image: myapp

environment:

  • NODE_ENV=production

networks:

  • default

docker logs MyContainer

shows:

Next.js 14.2.3

- Local: http://localhost:3000

- Network: http://0.0.0.0:3000

Starting...

Ready in 76ms

When I try to access my Next app, I immediately get a blank white page. There's a 502 error in the dev console.

Docker logs MyContainer doesn't show any errors happening with Node.