title: nodejs server docker container can't connect to mysql docker container (ECONNREFUSED)
body: <p>I have a docker setup with five containers:</p>
  <ul>
  <li>nginx</li>
  <li>php-fpm</li>
  <li>mysql</li>
  <li>mailhog</li>
  <li>websocket (nodejs server)</li>
  </ul>
  <p>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:</p>
  <pre><code>{ 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 }
  </code></pre>
  <p>I've checked the connection details and they are <strong>exactly</strong> the same as my PHP api which runs within the nginx/fpm containers:</p>
  <pre><code>var db = mysql.createPool({
          host: &quot;db&quot;,
          port: &quot;3360&quot;,
          user: &quot;root&quot;,
          password: &quot;pw&quot;,
          database: &quot;livechat&quot;
      });
  </code></pre>
  <p>Here is my docker-compose.yml:</p>
  <pre><code>version: '3'
  
  services:
    nginx:
      image: nginx:alpine
      depends_on:
        - &quot;db&quot;
        - &quot;fpm&quot;
        - &quot;websocket&quot;
      links:
        - fpm
      ports:
        - &quot;80:80&quot;
        - &quot;443:443&quot;
      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:
        - &quot;db&quot;
      volumes:
        - ./public:/var/www/live-chat-api
        - ./../live-chat/app-dist
      working_dir:
        /var/www/live-chat-api
  
    db:
      image: mysql:5.7
      ports:
        - &quot;3360:3306&quot;
      volumes:
        - data:/var/lib/mysql
      command: --sql_mode=&quot;&quot;
      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:
        - &quot;db&quot;
      links:
        - &quot;db&quot;
      volumes:
        - ./../live-chat-server:/usr/src/app
      working_dir:
        /usr/src/app
      ports:
        - &quot;3000:3000&quot;
  
      command: bash -c &quot;npm run dev&quot;
  
  volumes:
    data:
  </code></pre>
  <p>I've logged into the 'websocket' container and pinged the 'db' container successfully too:</p>
  <pre><code># 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
  
  </code></pre>
  <p>Why can't I connect to the mysql server from the websocket server?</p>
  <p>Also, these connection details are for a localised dev env. So i'm not too concerned about the 'security' of these logins.</p>