How do I create a next.js docker container without installing node on the host system
Unanswered
Pixiebob posted this in #help-forum
PixiebobOP
I've been trying to get next to run and I'm losing my hair.
npm install -g create-next-app@latest && \
npx create-next-app my-app --use-npm --js --no-tailwind --eslint --app --no-src-dir --no-turbo --yes --verbose --no-interactive
I tried using the above command within a docker container, but it still prompts for user input so I cannot run it within a dockerfile.
I also tried this method:
and everything seemed to run fine but, at no point does it seem to create any files within /app - theres a package.json, a package-lock.json, and a node_modules folder, but none of the other critical components exist anywhere on the container. What am I doing wrong?
npm install -g create-next-app@latest && \
npx create-next-app my-app --use-npm --js --no-tailwind --eslint --app --no-src-dir --no-turbo --yes --verbose --no-interactive
I tried using the above command within a docker container, but it still prompts for user input so I cannot run it within a dockerfile.
I also tried this method:
# Use the official Node.js image as the base image
FROM node:20
# Set the working directory
WORKDIR /app
# Create package.json and install dependencies
RUN npm init -y && \
npm install next@latest react@latest react-dom@latest eslint -y
# Add scripts to package.json
RUN sed -i 's/"scripts": {/"scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint",/' package.json
# Create the required folder structure and files
RUN mkdir -p app && \
echo "export default function RootLayout({ children }) { return (<html lang=\"en\"><body>{children}</body></html>); }" > app/layout.tsx && \
echo "export default function Page() { return <h1>Hello, Next.js!</h1>; }" > app/page.tsx && \
mkdir -p public
# Copy the rest of the application code
COPY . .
# Build the Next.js application
RUN npm run build
# Expose the port the app runs on
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
EOFand everything seemed to run fine but, at no point does it seem to create any files within /app - theres a package.json, a package-lock.json, and a node_modules folder, but none of the other critical components exist anywhere on the container. What am I doing wrong?
62 Replies
It might be a XY question. Why are you creating a app in docker container
@"use php" https://xyproblem.info/
PixiebobOP
The only goals and requirements I have are:
application with Node.js, Next.js, React, Express
unattended install
managed in some way, whether its by Docker, PM2, or Systemd, or something else just something.
The only successful way I've been able to perform an install of next.js is through npx create-react-app but I cannot get that to work in docker, and no matter what ive tried I cannot get next.js to run with systemd. As soon as the node server tries to require next it errors out and fails saying it cant be found, but that exact same process works without systemd if I run the commands manually. Create-react-app even has instructions on how to create a next.js app without it, and the instructions didn't work in docker.
application with Node.js, Next.js, React, Express
unattended install
managed in some way, whether its by Docker, PM2, or Systemd, or something else just something.
The only successful way I've been able to perform an install of next.js is through npx create-react-app but I cannot get that to work in docker, and no matter what ive tried I cannot get next.js to run with systemd. As soon as the node server tries to require next it errors out and fails saying it cant be found, but that exact same process works without systemd if I run the commands manually. Create-react-app even has instructions on how to create a next.js app without it, and the instructions didn't work in docker.
PixiebobOP
run next.js?
I ran this command:
It didn't ask for any inputs
$ npx create-next-app@latest ez-app --tailwind --eslint --app --src-dir --ts --no-import-aliasIt didn't ask for any inputs
you can customise the options
you prob didn't add
--no-import-aliasbut that makes zero sense in his case cause docker bundles a already made app
Yup
@gin but that makes zero sense in his case cause docker bundles a already made app
PixiebobOP
what is it? I kept trying to find it but everything I found online was just a node container with a command to install next that didnt work for me
@Pixiebob what is it? I kept trying to find it but everything I found online was just a node container with a command to install next that didnt work for me
so basically your Dockerfile is for ci/cd
either u bundle your nextjs app local
or u have it build for u with github actions or whatever
PixiebobOP
I have a private repo set up with the code
@Pixiebob I have a private repo set up with the code
Yeah take a look at github actions
Docker is meant for building and running apps
not creating apps
@gin Yeah take a look at github actions
PixiebobOP
not on github, my own server
gitlab selfhosted or what
Can you please just tell why are you creating app in docker?
@"use php" Docker is meant for building and running apps
PixiebobOP
I didn't originally want it to be in docker but I couldn't run it with systemd.
It's just a webserver; My script creates user accounts, configures a firewall, and sets up SSL certificates using Certbot for secure connections. The script also builds Docker containers for a Node.js/Next.js/React app, a PostgreSQL database, and a Git server, and it establishes a deployment script and Git hook for continuous integration. Finally, it configures a mail server.
It's just a webserver; My script creates user accounts, configures a firewall, and sets up SSL certificates using Certbot for secure connections. The script also builds Docker containers for a Node.js/Next.js/React app, a PostgreSQL database, and a Git server, and it establishes a deployment script and Git hook for continuous integration. Finally, it configures a mail server.
the next container is just the webserver redirected to by the nginx reverse proxy
a webserver without anything to display
PixiebobOP
I'm just trying to get it to display sample text before I care about putting a website to it
@Pixiebob I'm just trying to get it to display sample text before I care about putting a website to it
U dont do that tho... U modify your app locally or whatever and push it to your ci so it can be run in docker and be scaled
PixiebobOP
dont do what?
U dont deploy a empty nextjs app with docker
u cant even modify your container (for whatever reason) since u run your app in production
Docker is not designed for that
PixiebobOP
I have no idea what you're talking about, docker is not designed for uploading applications to??? what are you talking about
@gin U dont deploy a empty nextjs app with docker
PixiebobOP
I really am not understanding what you're referring to, what exactly is causing the big issue here
@"use php" But why can’t you run it with systemd ?
PixiebobOP
That I have no idea
@"use php" Not for creating applications
PixiebobOP
what do you mean??
@Pixiebob I really am not understanding what you're referring to, what exactly is causing the big issue here
Docker is not meant for creating apps, but for giving it a app and building and deploying it
PixiebobOP
thats... what?
@Pixiebob I really am not understanding what you're referring to, what exactly is causing the big issue here
The big issue is, that u obviously dont understand what Docker is
check this
PixiebobOP
I mean yeah duh but an "app" is just text
it literally comes from thin air it doesnt matter where it comes from, I just want next to run on a docker I dont care how it gets there
@"use php" I ran this command:
$ npx create-next-app@latest ez-app --tailwind --eslint --app --src-dir --ts --no-import-alias
It didn't ask for any inputs
If you don’t care at all, here’s a command, but I don’t recommend running it on docker
@"use php" If you don’t care at all, here’s a command, but I don’t recommend running it on docker
PixiebobOP
I sitll have no idea what you're talking about
whats so spooky and taboo that you both are acting like I have a 6 foot tall spider on my back
and cant even tell me whats wrong
and again, none of this helps me get next.js running on docker
or on ubuntu at all in any way in a manged state
Russian Spaniel
dude, do you have teamviewer or anything like this installed?
or want to do a call and screenshare?
or how about starting with the basics. what do you want to achieve?
@Pixiebob or on ubuntu at all in any way in a manged state
There are many ways to run Next.js on a server. Pm2 is one of the easiest ways
@averydelusionalperson There are many ways to run Next.js on a server. Pm2 is one of the easiest ways
PixiebobOP
pm2 was the first thing I tried
@Russian Spaniel or want to do a call and screenshare?
PixiebobOP
I think that would help alot
also is that paul from tekken
@Pixiebob pm2 was the first thing I tried
What went wrong then
@Pixiebob I think that would help alot
Russian Spaniel
shall we take this to dm? because I don't usually help out here and my time is quite limited.