I built a React frontend with Node.js backend that runs perfectly on my local machine. However, after containerizing both parts and using docker-compose, I can only access the app through localhost but not through the network IP address.
My project structure:
--server(backend folder)
--dockerfile
--client(frontend folder)
--dockerfile
--docker-compose.yml
Frontend Dockerfile:
FROM node:12
WORKDIR /client
COPY . .
RUN npm install && npm run build
EXPOSE 3000
CMD ["npm", "start"]
Backend Dockerfile:
FROM node:12
WORKDIR /server
COPY ./package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["node", "index.js"]
Docker-compose configuration:
version: '3.8'
services:
web-server:
build: ./server
container_name: web-server
ports:
- "5000:5000"
web-client:
depends_on:
- web-server
build: ./client
container_name: web-client
ports:
- "3000:3000"
tty: true
Client package.json includes:
{
"name": "web-client",
"version": "0.1.0",
"proxy": "http://web-server:5000",
"dependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-scripts": "4.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build"
}
}
Server listens on port 5000:
const port = 5000;
server.listen(port, () => {
console.log(`Application running on port ${port}`);
});
When I run docker-compose up, the output shows:
You can now view web-client in the browser.
Local: http://localhost:3000
On Your Network: http://172.18.0.4:3000
The localhost URL works fine, but the network IP (172.18.0.4:3000) gives me a connection error. What could be causing this network accessibility issue?