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

Time:11-24

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

CodePudding user response:

Okay. You are running a docker in a wsl. WSL is a virtual box on top of windows os. So it has its own network adapters. So please find the wsl's IP by the "ifconfig" command. It will tell you wsl's IP.

Then you can connect that IP from your browser

http://wsl-ip:4000

CodePudding user response:

You have to expose your nodejs port inside the container. you just open host port 4000 to the container but container not listening in port 4000, for that you have to changein your Dockerfile

changed Dockerfile

# 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 . ./

EXPOSE 4000

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

  • Related