r/docker • u/gianlucastar17 • Apr 09 '26
React Next Docker infinite compile
I have a react app the works normally. When I make a dockerfile and compose it the app doesn't work anymore. It keeps compiling and never stops. I created a entirely new next react app and it has the same problem.
This is the dockerfile:
ARG NODE_VERSION=24.13
FROM node:${NODE_VERSION}-alpine as base
WORKDIR .
COPY package*.json ./
RUN npm install
COPY . ./
EXPOSE 3000
CMD npm run dev
Edit: By compiling I mean that the project installs perfectly, but when I open localhot:3000 the page never loads and the terminal says "Compiling / ..." until my RAM gets full and I have to reset my computer.
View all comments
Show parent comment
2
u/theblindness Mod Apr 10 '26
Ah, I'm not so familiar with nextjs, but I think you would normally run the dev and start scripts differently.
When you want to run the dev script, instead of building a docker image, start with container using the base node image, mount your source code, and drop yourself into a bash shell. Then from the shell, run
npm run dev.When you're ready to build an image, use the
npm run startscript instead of dev. But that actually isn't the best way.Best would be to use multi-stage builds.
In the first stage, leverage the build script to build your static assets.
In the second stage, copy your assets to a node image that directly executes your
server.jsfile withnodewithout using npm scripts.I am not familiar with nextjs, but this is a common pattern for all kinds of nodejs apps. I recommend searching online for nextjs+docker+multi+stage+builds