Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access Node.js app running in docker through browser

I am trying to run a Node.js app on docker. I am using WSL and have successfully run the app on docker. Now the problem is that when I try to access the app on the browser, it's showing as no response. I can't access it in the browser. I also have another Reactjs app running on the docker and I can access that app through the browser. What am I doing wrong? Why Nodejs app is not accessible in the windows browser? Below is the Dockerfile for Node.js

# pull official base image
FROM node:12.18.3-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent

# add app
COPY . ./


# start app
CMD ["npm", "run", "dev"]

Below is the command used for running the app image in docker

docker build -t gdns/node-app .

winpty docker run \
    -it \
    --rm \
    -v ${PWD}:/app \
    -v /app/node_modules \
    -p 4000:4000 \
    -e CHOKIDAR_USEPOLLING=true \
    gdns/node-app

Node.js app is running, I can view the log as below

enter image description here

Please help

like image 503
Akhilesh Avatar asked Oct 24 '25 03:10

Akhilesh


1 Answers

I have changed the code in the Node.js app to listen on "0.0.0.0".

const hostname = '0.0.0.0';
const port = 4000;
server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});
like image 96
Akhilesh Avatar answered Oct 26 '25 17:10

Akhilesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!