Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node server on docker not accessible on localhost even after port binding

I have graphql node server. I am able to run it locally without docker. But after creating a docker container of the server and binding the container port with the host port this doesn't work.

Here's my Dockerfile code:

 FROM node:boron-alpine
 WORKDIR /app
 COPY package.json /app
 RUN npm install
 COPY . /app
 ENV SERVER_PORT 8080 
 EXPOSE 8080
 CMD npm run build && npm start

My node server code is as such :

  app.listen(process.env.SERVER_PORT,'0.0.0.0')
  console.log(`listening at ${port}`)

I run docker with the following command:

   docker run -it -p 8080:8080  nodeapi

This works perfectly nodejs can see the env SERVER_PORT cause it shows

   "listening at 8080"

in the console.

But when i open localhost:8080 this doesn't work (The site cannot be reached).

I have also tried running docker command

    docker run -it -p 127.0.0.1:8080:8080  nodeapi

This doesn't work

I am using docker toolbox on windows 10 latest build

Thank you

like image 369
scroobius Avatar asked Oct 12 '17 21:10

scroobius


3 Answers

Docker toolbox doesn't map ports to localhost. It maps it to the Docker VM IP's

Run below command to get the IP

docker-machine ip

Then use the http://<IP>:8080 in your browser

like image 57
Tarun Lalwani Avatar answered Nov 17 '22 09:11

Tarun Lalwani


If you avoid specifying the IP 0.0.0.0

app.listen(process.env.SERVER_PORT, function () {
   console.log('Listening on port '+ process.env.SERVER_PORT);
});

and

docker run -it -p 8080:8080  nodeapi

it will let you load the site as

http://localhost:8080
like image 6
nimo Avatar answered Nov 17 '22 07:11

nimo


when you start the Docker using docker quickstart terminal, you can see the IP Address. (you can get to know the ip-address using the command "docker-machine ip" as well)

URL to access will be http://ip-address:portbinded

like image 1
neeraj-dixit27 Avatar answered Nov 17 '22 07:11

neeraj-dixit27