0

I'm currently running a local instance of RocketChat and the RocketBot using docker-compose and a corresponding docker-compose.yaml file:

I use the standard mysql module like this:

var con = mysql.createConnection({
    host: '<placeholder>',
    user: 'root',
    port: '3306',
    password: '<placeholder>',
});

The host, user, port and password are gathered from running the inspect command on the container containing the MySQL server. The MySQL does work as I can run it and make changes to it and even connect to it using MySQL workbench. I get this error:

rosbot_1              | [Tue Jun 18 2019 18:42:06 GMT+0000 (UTC)] ERROR Error: connect ETIMEDOUT
rosbot_1              |     at Connection._handleConnectTimeout (/home/hubot/node_modules/mysql/lib/Connection.js:412:13)

I have no idea how to proceed now, how can I connect from the bot served by docker-compose to the MySQL container using JavaScript?

EDIT:

docker-compose.yaml:

version: '2.1'

services:
  mongo:
    image: mongo:3.2
    hostname: 'mongo'
    volumes:
     - ./db/data:/data/db
     - ./db/dump:/dump
    command: mongod --smallfiles --oplogSize 128 --replSet rs0

  mongo-init-replica:
    image: mongo:3.2
    command: 'mongo mongo/rocketchat --eval "rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''localhost:27017'' } ]})"'
    links:
      - mongo:mongo

  rocketchat:
    image: rocketchat/rocket.chat:latest
    hostname: 'rocketchat'
    volumes:
      - ./rocketchat/uploads:/app/uploads
    environment:
      - PORT=3000
      - ROOT_URL=http://localhost:3000
      - MONGO_URL=<placeholder>
      - MONGO_OPLOG_URL=<placeholder>
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 30s
      timeout: 10s
      retries: 5
    links:
      - mongo:mongo
    ports:
      - 3000:3000

  <placeholder>:
    image: <placeholder>
    hostname: "<placeholder>"
    environment:
      - ROCKETCHAT_URL=<placeholder>
      - ROCKETCHAT_ROOM=""
      - ROCKETCHAT_USER=<placeholder>
      - ROCKETCHAT_PASSWORD=<placeholder>
      - ROCKETCHAT_AUTH=<placeholder>
      - BOT_NAME=<placeholder>
      - LISTEN_ON_ALL_PUBLIC=true
      - EXTERNAL_SCRIPTS=hubot-help,hubot-seen,hubot-links,hubot-diagnostics,hubot-pugme,hubot-reload
      - PENTEXT_PATH=/home/hubot/pentext
      - ADDITIONAL_PACKAGES=mysql,lodash
      - RESPOND_TO_LIVECHAT=true
      - RESPOND_TO_DM=true
    depends_on:
      rocketchat:
        condition: service_healthy
    links:
      - rocketchat:rocketchat
    volumes:
      - <placeholder>
    ports:
      - 3001:3001
PEREZje
  • 2,272
  • 3
  • 9
  • 23
  • Choudl you share the docker-compose.yml file ? – F.Igor Jun 18 '19 at 18:55
  • Have added it to the question, have redacted some of the sensitive stuff that contained the names of the project in question. The refers to the bot in the rocket chat. – PEREZje Jun 18 '19 at 18:58
  • It looks like the mysql container is not a standalone container or is not part of this docker-compose configuration. Is mysql in another container, or is included in some container listed in your file? – F.Igor Jun 18 '19 at 19:08
  • @F.Igor I'm running the MySQL server in another docker container yes not in this docker-compose file. – PEREZje Jun 18 '19 at 19:08

1 Answers1

0

Normally, you can connect to another container using the container name as hostname:

If you have a container with mysql, the container name (in this example 'db') is the host name to access the mysql container (also, you can use a hostname: 'mysqlhostname' to specify a different name):

db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mypass
      MYSQL_DATABASE: mydb

In your rocketchat container you should add some environment variables for mysq root password and database to make it available to your container

rocketchat:
    image: rocketchat/rocket.chat:latest
    hostname: 'rocketchat'
    volumes:
      - ./rocketchat/uploads:/app/uploads
    environment:
      - PORT=3000
      - ROOT_URL=http://localhost:3000
      - MONGO_URL=<placeholder>
      - MONGO_OPLOG_URL=<placeholder>
      - MYSQL_ROOT_PASSWORD: mypass
      - MYSQL_DATABASE: mydb
      - MYSQL_HOSTNAME: db
    ...
    links:
      - rocketchat:rocketchat 
      - db : db

And then, use the host name and the environment variables to create your connection:

var con = mysql.createConnection({
    host: 'db', // or process.env.MYSQL_HOSTNAME
    user: 'root',
    port: '3306',
    password: 'mypass', // or process.env.MYSQL_ROOT_PASSWORD
});
F.Igor
  • 4,119
  • 1
  • 18
  • 26
  • When I add this I get the error: ERROR Error: connect ECONNREFUSED 127.0.1:3306. I also had to add quotation marks around the new arguments for it to even serve the file. – PEREZje Jun 18 '19 at 19:38
  • I forgot to add the host to the "links" section (now i've edited to add db in `links:` section of the example) – F.Igor Jun 18 '19 at 19:59
  • I now seem to get a different errror: ERROR Error: ER_NOT_SUPPORTED_AUTH_MODE. I get this error if I don't add the variables to the environment under rocketchat. Thanks for the help thus far, I would be lost without it. – PEREZje Jun 18 '19 at 20:25
  • If you are getting errors related to authentication modes in mysql, you should check the docker mysql reference to modify some authentication parameters. For example, adding `command: --default-authentication-plugin=mysql_native_password` (see https://hub.docker.com/_/mysql) – F.Igor Jun 18 '19 at 20:32
  • Additionally, check https://stackoverflow.com/questions/44946270/er-not-supported-auth-mode-mysql-server for a different solution (modifying the root user to use native password) – F.Igor Jun 18 '19 at 20:35