2

When I try to connect two docker containers on the same machine, one running a node.js server and the other running mysql dbms

I get the following error:

(node:32) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND jdbc:mysql://localhost:3306 jdbc:mysql://localhost:3306:3306

mysql driver connection config

const connection= mysql.createConnection({
  host: 'jdbc:mysql://topsectiondb:3306',
  user: 'root',
  password: 'rootpass' 
})

docker-compose.yml

version: '3'
services:
  topsection:
    container_name: topsection-server
    restart: always
    build: .
    ports:
      - '7777:7777'
    depends_on: 
      - topsectiondb
    environment:
      - PORT=7777
  topsectiondb:
    container_name: topsectiondb
    image: mysql:8.0.3
    ports:
      - '3306:3306'
    environment:
      - MYSQL_ROOT_PASSWORD=rootpass

Dockerfile

FROM node:10

RUN mkdir serviceFolder

WORKDIR /usr/app/

COPY . . 

RUN npm install

EXPOSE 7777

CMD ["npm", "start"]

for a more complete stack trace https://gist.github.com/armouti/877a8b4405330c44e4009ebae3df822c

  • Your error just means that URL you're trying to make the connection to doesn't exist. I.e. it's not running on the localhost of the nodejs container. – mortuie Nov 11 '18 at 16:09
  • Possible duplicate of [Unable to Connect MySQL container to Tomcat Container in docker](https://stackoverflow.com/questions/35510964/unable-to-connect-mysql-container-to-tomcat-container-in-docker) – David Maze Nov 11 '18 at 16:10
  • @DavidMaze the answer in that question are somewhat outdated, they use the legacy --link – Ismail Armouti Nov 11 '18 at 16:25

1 Answers1

5

First of all there are a couple of problems here.

Firstly you need to look at networking your docker containers in your docker-compose file together. Basically each container doesn't know of the other until you specify which network they are in. Each container can be in multiple but if you want two containers to connect they need to be in the same network. https://docs.docker.com/compose/networking/#configure-the-default-network

Then Secondly you can't use localhost as a url. Basically a docker container is an isolated environment so it has it's own "localhost" so when you use that address it's actually trying to connect to the mysql on the nodejs container. So when networked properly you will be able to connect to the mysql container using their names you gave them in the docker-compose something like topsectiondb:3306 or something like this. This tells docker your using networking to connect one docker container to another and will allow you to make the initial connection. Connect to another container using Docker compose

===================================================

Actual Answer:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

Basically the mysql library requires a hostname, which in most cases is localhost, but as your linking containers with docker-compose you use the alias of the container. The port number is 3306 by default

So: host: "topsectiondb" will suffice!

mortuie
  • 325
  • 3
  • 11
  • 2
    Current versions of Docker Compose will automatically create a Docker network for you, and will automatically register containers under their block name from the `docker-compose.yml` file. You don't need any special configuration for one Compose-managed container to reach another by host name. – David Maze Nov 11 '18 at 16:11
  • topsectiondb:3306 is actually how i had it earlier but i changed it because it wasn't working. And Like David maze said, no need to add the containers to a network explicitly, which i actually did just to be more explicit and did not work – Ismail Armouti Nov 11 '18 at 16:16
  • Like this: `'jdbc:mysql://topsectiondb:3306',` ?? – mortuie Nov 11 '18 at 16:17
  • @LeonBoehmer yeah with the exact same log output – Ismail Armouti Nov 11 '18 at 16:19
  • 1
    can you `docker exec` into the container and ping the db container? – mortuie Nov 11 '18 at 16:28
  • `var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); });` also why are you using a jdbc string??? jdbc is a java database connection string and not useful in node? try `topsectiondb:3306` – mortuie Nov 11 '18 at 16:33
  • Sorry use "topsectiondb" as your host and don't worry about the port the mysql package default port is set to 3306 – mortuie Nov 11 '18 at 16:35
  • your last comment was the answer!! i did ping the server but it only works when i omit the port number, same with the mysql connection in the node server. Thank! you should move it to an answer – Ismail Armouti Nov 11 '18 at 17:08