0

I have a running service within a container on my localhost machine I Can't establish a connection with it even though I specified the port mapping within my docker-compose.yml here it is

version: '2.1'

services:

  users-db:
    container_name: users-db
    build: ./services/users-service/src/db
    ports: 
      - '27017:27017'
    volumes:
      - './services/users-service/src/db/:/data/db'

  users-service:
    container_name: users-service
    build: './services/users-service/'
    volumes:
      - './services/users-service:/usr/src/app'
      - './services/users-service/package.json:/usr/src/package.json'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=test
      - JWT_SECRET=fuckOffChinese
    depends_on:
      users-db:
        condition: service_started

  presence_db:
    image: redis
  presense_service:
    container_name: presense_service
    build: './services/presence-service/'
    ports:
      - "8081:8081"
    environment:
      - JWT_SECRET=thirdEyeSecret
      - PORT=8081
    volumes:
      - './services/presence-service:/usr/src/app'
      - './services/presence-service/package.json:/usr/src/package.json'
    depends_on:
      - presence_db

this is the command that I use to run this service

docker-compose run presense_service

and this is what I get every time I try to ping it from the terminal by simply doing an HTTP GET request

http: error: ConnectionError: HTTPConnectionPool(host='localhost', port=8081): Max retries exceeded with url: / (Caused by

NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused',)) while doing GET request to URL: http://localhost:8081/

I'm running macOS 10.13.5 and the server start noramally and here is the logs of it

> [email protected] start /usr/src
> gulp --gulpfile app/gulpfile.js

[10:12:46] Working directory changed to /usr/src/app
[10:12:52] Using gulpfile /usr/src/app/gulpfile.js
[10:12:52] Starting 'start'...
[10:12:56] Finished 'start' after 3.99 s
[10:12:56] Starting 'lint'...
[10:12:57] Finished 'lint' after 239 ms
[10:12:57] Starting 'default'...
[10:12:57] Finished 'default' after 131 μs
[10:12:57] [nodemon] 1.18.2
[10:12:57] [nodemon] to restart at any time, enter `rs`
[10:12:57] [nodemon] watching: *.*
[10:12:57] [nodemon] starting `node ./index.js`
Server listening on: http://localhost:8081
Redis client connected
6
  • Are you sure the server is up and running? Commented Jul 15, 2018 at 17:30
  • yes of course i can see the logs Commented Jul 15, 2018 at 17:51
  • What's your host OS? How did you install Docker? Commented Jul 15, 2018 at 23:49
  • @DavidMaze my host os is macOS 10.13.5 and I install docker normally by installing .dmg instance from the website i guess Commented Jul 16, 2018 at 8:40
  • When the server starts, does it print something like "listening on 0.0.0.0:8081"? (If the server isn't actually listening on that port, or if it's bound to 127.0.0.1 within the container, that would cause this) Commented Jul 16, 2018 at 9:45

3 Answers 3

2

Try docker-compose run presense_service --service-ports, or better, use docker-compose up.

docker-compose run specifically doesn't apply the ports from your Compose file to "prevent port collisions with already-open ports" [1] - so you have to add this option, or specify them manually with the same options you would pass to docker run.

Ideally, use docker-compose up -d and then docker-compose logs -f presense_service to get logs. Shut your application down with docker-compose down.

If you really need to, you can comment services out of your docker-compose.yml file that you don't want started.

If you didn't know, the latest version of the compose format is 3.6 - 2.1 is over two years old (released with Docker v1.12.0 on 201607/28 [2]).


Proving this is easy - since I didn't have your code, I replaced all the image/build lines with image: nginx (and took the host path off any volumes).

Example modified compose file (just for reference):

version: '2.1'

services:

  users-db:
    container_name: users-db
    image: nginx
    ports:
      - '27017:27017'
    volumes:
      - './services/users-service/src/db/:/data/db'

  users-service:
    container_name: users-service
    image: nginx
    volumes:
      - '/usr/src/app'
      - '/usr/src/package.json'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=test
      - JWT_SECRET=fuckOffChinese
    depends_on:
      users-db:
        condition: service_started

  presence_db:
    image: redis
  presense_service:
    container_name: presense_service
    image: nginx
    ports:
      - "8081:8081"
    environment:
      - JWT_SECRET=thirdEyeSecret
      - PORT=8081
    volumes:
      - '/usr/src/app'
      - '/usr/src/package.json'
    depends_on:
      - presence_db

Running docker ps after docker-compose up -d gives this (pay attention to the PORTS column):

my-machine$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
f1b89cf3d6c2        nginx               "nginx -g 'daemon of…"   19 seconds ago      Up 14 seconds       80/tcp, 0.0.0.0:3000->3000/tcp     users-service
be0e9b2bb005        nginx               "nginx -g 'daemon of…"   19 seconds ago      Up 13 seconds       80/tcp, 0.0.0.0:8081->8081/tcp     presense_service
2efed2546926        nginx               "nginx -g 'daemon of…"   20 seconds ago      Up 14 seconds       80/tcp, 0.0.0.0:27017->27017/tcp   users-db
c7a88a84f422        redis               "docker-entrypoint.s…"   20 seconds ago      Up 14 seconds       6379/tcp                           test_presence_db_1

...so there's nothing wrong with your port configuration. docker ps after docker-compose run presense_service, then, shows:

my-machine$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
89e4a4f90a75        nginx               "nginx -g 'daemon of…"   28 seconds ago      Up 21 seconds       80/tcp              test_presense_service_run_1
2c91fcb5091d        redis               "docker-entrypoint.s…"   29 seconds ago      Up 24 seconds       6379/tcp            test_presence_db_1

...and therefore it was your command causing the problem. Happy to help as I've learnt something new :)


[1] https://docs.docker.com/compose/reference/run/

[2] https://docs.docker.com/release-notes/docker-engine/#1120-2016-07-28

Sign up to request clarification or add additional context in comments.

Comments

0

Try this to find out if port 8081 is open/occupied:

netstat -l | grep 8081

Comments

0

When your server logs say

Server listening on: http://localhost:8081

The server will be inaccessible outside the Docker container. It looks like this is the default behavior of the Express JavaScript Web server, and many other frameworks when run in “developer” mode. You need to set the server to listen on all IP addresses, probably by passing 0.0.0.0 as the “bind” address. (Note that you cannot connect to 0.0.0.0, and saying “listen to 0.0.0.0” means “accept connections from anywhere”.

If your server is in fact based on Express, https://superuser.com/questions/582624/how-to-access-nodejs-server-on-lan might be informative to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.