How to install the node in Dockerfile?

I want to install the node v18 in on AWS Linux


  • I have Django and frontend React system. So, I want to use node when installing frontend.
  • If I use make Dockerfile such as From node:18 it works, but I want to use FROM python:3.9 to django work.
  • Is it not a good idea to put Djang and React in the same container?

Now my docker file is like this.

FROM python:3.9
RUN apt-get update 
WORKDIR /usr/src/app
RUN apt-get install -y npm
RUN pip install pipenv
WORKDIR /usr/src/app/frontend_react
RUN npm install --force
RUN node -v //version 12 is installed
RUN dnf module install nodejs:18/common
RUN node -v
RUN npm run build

Howeber there is no dnf.

How can I do this?

If you can use prebuilt Docker Hub images then this is much easier. I would generally avoid trying to put components with different use cases, build systems, and runtimes into the same image if possible.

In the specific case of a Django application with a React frontend, you might be compiling the frontend to static files that you then serve directly via Django. In this setup, you don't need Node to run the application, just so long as the static files exist then Django can serve them up. Docker's multi-stage build feature will let you build the front-end using a node image, then COPY it into your application. A typical example might look like:

FROM node:18 AS react
COPY frontend_react/package*.json ./
RUN npm ci
COPY fronend_react/ ./
RUN npm build

FROM python:3.9
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./
COPY --from=react /app/dist/ frontend_react/dist/
CMD ["./", "runserver", ""]

The first half should look like a normal React image build, except that it doesn't have a CMD. The second half should look like a normal Django image build, plus the COPY --from=react line to get the built application from the first build stage. We don't need node or npm in the final image, only the static files, and so we don't invoke a package manager to try to install them.

Back to Top