0

I have a docker setup with five containers:

  • nginx
  • php-fpm
  • mysql
  • mailhog
  • websocket (nodejs server)

I have only recently migrated the websocket server into a docker container and all is working fine, apart from the database connection. Everytime I attempt to run a query, I get the following error:

{ Error: connect ECONNREFUSED 172.20.0.2:3360

at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)

--------------------

at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)

at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)

at PoolConnection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:116:18)

at Pool.getConnection (/usr/src/app/node_modules/mysql/lib/Pool.js:48:16)

at Pool.query (/usr/src/app/node_modules/mysql/lib/Pool.js:202:8)

at Object.exports.getRoom (/usr/src/app/db.js:30:8)

at Socket.socket.on (/usr/src/app/index.js:175:16)

/usr/src/app/db.js:35

at Socket.emit (events.js:198:13)

at /usr/src/app/node_modules/socket.io/lib/socket.js:528:12

at process._tickCallback (internal/process/next_tick.js:61:11)

errno: 'ECONNREFUSED',

code: 'ECONNREFUSED',

syscall: 'connect',

address: '172.20.0.2',

port: 3360,

fatal: true }

I've checked the connection details and they are exactly the same as my PHP api which runs within the nginx/fpm containers:

var db = mysql.createPool({
        host: "db",
        port: "3360",
        user: "root",
        password: "pw",
        database: "livechat"
    });

Here is my docker-compose.yml:

version: '3'

services:
  nginx:
    image: nginx:alpine
    depends_on:
      - "db"
      - "fpm"
      - "websocket"
    links:
      - fpm
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./public:/var/www/live-chat-api
      - ./../live-chat/app-dist:/var/www/live-chat-app
      - ./docker/nginx/vhost.conf:/etc/nginx/conf.d/live-chat-api.conf
      - ./docker/ssl:/etc/ssl
    working_dir:
      /var/www/live-chat-api

  fpm:
    build: ./docker/php-fpm
    links:
      - "db"
    volumes:
      - ./public:/var/www/live-chat-api
      - ./../live-chat/app-dist
    working_dir:
      /var/www/live-chat-api

  db:
    image: mysql:5.7
    ports:
      - "3360:3306"
    volumes:
      - data:/var/lib/mysql
    command: --sql_mode=""
    environment:
      MYSQL_ROOT_PASSWORD: pw
      MYSQL_DATABASE: livechat
      MYSQL_USER: root
      MYSQL_PASSWORD: pw

  mailhog:
    image: mailhog/mailhog:latest
    logging:
      driver: 'none'  # disable saving logs
    ports:
      - 1025:1025
      - 8025:8025

  websocket:
    image: node:10
    depends_on:
      - "db"
    links:
      - "db"
    volumes:
      - ./../live-chat-server:/usr/src/app
    working_dir:
      /usr/src/app
    ports:
      - "3000:3000"

    command: bash -c "npm run dev"

volumes:
  data:

I've logged into the 'websocket' container and pinged the 'db' container successfully too:

# ping db
PING db (172.20.0.2) 56(84) bytes of data.
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=1 ttl=64 time=0.159 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=2 ttl=64 time=0.133 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=3 ttl=64 time=0.113 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=4 ttl=64 time=0.136 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=5 ttl=64 time=0.163 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=6 ttl=64 time=0.163 ms
64 bytes from live-chat-api_db_1.live-chat-api_default (172.20.0.2): icmp_seq=7 ttl=64 time=0.232 ms
^C
--- db ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6153ms
rtt min/avg/max/mdev = 0.113/0.157/0.232/0.035 ms

Why can't I connect to the mysql server from the websocket server?

Also, these connection details are for a localised dev env. So i'm not too concerned about the 'security' of these logins.

David
  • 16,246
  • 34
  • 103
  • 162
  • Does MySQL can be accessed from fpm container? May be MySQL listen only localhost? – Slava Rozhnev Nov 06 '20 at 08:31
  • @SlavaRozhnev Yes, my php scripts connect using the exact same connection details. They aren't connecting via 'localhost', instead they use the docker network name of 'db'. Localhost won't work as it's a different container. – David Nov 06 '20 at 08:37
  • Strangly, if I change the port to 3306 on the node server connection, it works...why doesn't it work with the exposed port instead? – David Nov 06 '20 at 10:12
  • @David seems like an issue with the nodejs mysql package. See https://stackoverflow.com/questions/21206801/node-js-mysql-error-econnrefused – jokarl Nov 06 '20 at 10:14
  • Inter-container connections completely ignore `ports:`. You need to use the standard MySQL port 3306; it doesn't matter that `ports:` remaps it externally to a different number. – David Maze Nov 06 '20 at 12:14

0 Answers0