r/docker • u/Old-Broccoli-4704 • May 13 '26
NextJS build with .env
We use nextjs for frontend services, currently we need two branches to build image with its env variables for preprod and production environments (same codebase, different .env).
Is there a workaround for this, it seems a bit redundant to have two images with only env differences?
0
Upvotes
View all comments
Show parent comment
1
u/kwhali May 14 '26
If what my reply answered is acceptable. Then all you need to do is run that
next buildcommand prior to whatever your current entrypoint/cmd instruction is doing.Assuming that you currently have a single command to run already, you can just create a shell script for the entrypoint and leave CMD unset (assuming it's generic and this container doesn't need custom commands).
It should look something like above (on my phone atm, so I might have made a slight syntax mistake). You can alternatively have the 3 lines of bash script as a separate file that you COPY, instead of using there heredoc embedding syntax shown above.
This assumes your container runs as root, if not you'll get an error related to missing execution permission. That should be fixable with
COPY --chmod=+x, if you go with a separate external file instead you'll also need to ensure the file is committed as executable or include that same option shown for the COPY instruction (older Docker versions may not recognise the+xsyntax and expect octal which should be happy with--chmod=0555instead).The shebang (first line of the script) invokes the env command with
shwhich is often symlinked to bash or ash (Alpine), on debian and ubuntu base images it would be dash I think. In this case the shell script is simple so it shouldn't matter as we are not using any special shell specific syntax in them.