I have created a MERN app with MongoDB Atlas such that node runs on port 3000 and react runs on 3001 and connected the node end points to react.
the Docker file for react is
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["npm", "start"]
and for nodeJS is
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
now the compose file is
services:
react-app:
image: react-app
build: ./client
stdin_open: true
ports:
- "3001:3001"
networks:
- mern-app
node-web-api:
image: node-web-api
build: ./server
ports:
- "3000:3000"
networks:
- mern-app
networks:
mern-app:
driver: bridge
now when i am composing it everything is workfine it composing sucessfully but the react is running on port 3000 instead of 3001
how can i fix this issue??
2
Answers
npm default port is 3000, so if you want to run in on another port you should change the command to:
It’s fine to have multiple processes running in containers listening on the same port. The Docker-level settings you show in the question have no effect on whatever
listen()
call is in the application. (@MojtabaAhadi’s answer discusses setting Node to listen on a different port.)What you can do instead is set the application to listen on its normal port, but remap that in the Compose setup. In the Dockerfile, change:
to match the port the application actually listens on. (Or not, or delete this line entirely; it doesn’t actually do anything in modern Docker.)
In the Compose file, for that container, the second
ports:
number needs to match what the application is listening on, and the first number is a host port that isn’t otherwise in use. They don’t need to be the same.If host port 3001 is busy then you can change that first port number only without rebuilding your image or changing anything else in the container setup.