YanivN
YanivN

Reputation: 391

Nodejs port with docker is unreachable

I'm trying to run node inside a docker container and to expose externally port 8585. I just want to test the accessibility of the exposed port.

For now, in order to simplify the problem, I excluded Nginx from this setup, I just want to test the nodejs+docker port.

The setup:

Nodejs:

const PORT = 8585;
const HOST = '127.0.0.1';
app.listen(PORT, HOST);

Dockerfile:

FROM node:11.10.1
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY . .
RUN npm install --production --silent
EXPOSE 8585
CMD node index.js

docker-compose.yml

version: '2.1'

services:
  mws-client:
    image: mws-client
    build: .
    environment:
      NODE_ENV: production
    ports:
      - '8585'

Running the docker image:

docker run -p 8585:8585 --expose 8585 1085d876c882

Running output:

$ docker run -p 8585:8585 --expose 8585 1085d876c882
About 2 run on http://127.0.0.1:8585
Running on http://127.0.0.1:8585

Netstat output:

$ netstat -a | grep 8585
tcp46      0      0  *.8585                 *.*                    LISTEN    

Docker ps:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
99d032ce1ad2        1085d876c882        "/bin/sh -c 'node in…"   7 minutes ago       Up 7 minutes        0.0.0.0:8585->8585/tcp   inspiring_hypatia

Still - no connection:

$ curl http://127.0.0.1:8585
curl: (52) Empty reply from server
$ curl http://127.0.0.1:80
<html><body><h1>It works!</h1></body></html>

Please help - clearly I am missing something fundamental (believe me I tested it with so many options..)

Upvotes: 1

Views: 1019

Answers (1)

Adiii
Adiii

Reputation: 60104

The issue is not with Docker or Host, The issue is in the node application.

you should not bind server to listen 127.0.0.1.

change your code and bind to 0.0.0.0 and everything will work fine.

const PORT = 8585;
const HOST = '0.0.0.0';
app.listen(PORT, HOST);

you are good to test

docker run -p 8585:8585 -it 1085d876c882

update compose

    ports:
      - "8585:8585" 

Upvotes: 3

Related Questions