How to dockerize a React app with Nest JS server code...!
Tech blog on dockerizing React app with one of Node's typescript framework Nest JS
VenkataGuruPrasad S
Namaste coders :) Welcome to my tech blog on dockerizing React app with one of Node's typescript framework. This is my first ever post in Tealfeed, excited to contribute it 😃.
Basically, there are two ways you can dockerize them,
1. Dockerize both React app and Nest JS separately and compose them.
2. Dockerize both of the apps in a single docker file.
1.Dockerize both React app and Nest JS separately and compose them.
a). Dockerize React app :
Create a docker file as below in React app -
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]`
Also create a .dockerignore file
.git
.gitignore
Next step is that we have to build the docker image of the React app.
Now run the tagged image as below.
Open http://localhost:3000 and you should see React served from Docker. Also you can check the docker container running as below with docker ps command.
6aea1cf12647 react "docker-entrypoint.s…" 11 days ago Up 3 seconds 0.0.0.0:80->3000/tcp, :::80->3000/tcp react
b). Dockerize Nest JS code :
Create a docker file as below in your server directory-
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD [ "npm", "run", "start:dev" ]
As similar to above create a .dockerignore file
.git
.gitignore
Next step is that we have to build the docker image of the server app.
Now run the tagged image as below.
Let's check this by hitting http://localhost:5000 and you should see your Nest JS being served from Docker.
So, now we have stepped into the final process of running both simultaneously by creating docker compose yaml file in the project root directory as below.
services:
frontend:
container_name: client
build:
context: ./client
dockerfile: Dockerfile
image: react
ports:
- "3000:3000"
volumes:
- ./client:/app
backend:
container_name: backend
build:
context: ./server
dockerfile: Dockerfile
image: server
ports:
- "5000:5000"
volumes:
- ./server:/app
Run the command docker-compose up and you should see both the apps running.
2. Dockerize both of the apps in a single docker file.
I would recommend this approach than the above, it's simple and preferred to follow for deploying these kind of applications for dev, qa and prod environments.
As we have both apps React and Nest JS server code. We initially need to build our React UI code and should copy the build folder contents as below -

After, we need to paste them in public folder of the Nest JS server code root directory.Note: In Nest JS, you need to place server static module in app.module.ts file as below -
Finally, you are all set to run the docker file with commands below -
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN cd ./client && npm ci && npm run build && cd ..
RUN cd ./server && npm ci && cd ..
RUN mkdir -p /usr/src/app/server/public
RUN cp -r ./client/build/* ./server/public/
WORKDIR /usr/src/app/server
RUN npm run prebuild
RUN npm run build
EXPOSE 5000
CMD [ "npm", "run", "start:dev" ]
Build the docker file
And finally run the image
Open http://localhost:5000 and you should see the application served from Docker.Congratulations you successfully dockerized React UI and Nestjs server...🎉🎉🎉
🚀 If you read something interesting from this article, please like and follow me for more posts.
Upvote
VenkataGuruPrasad S
Full stack developer with 4 years experience, worked on various technologies including UI, service and cloud.

Related Articles