将在Docker容器中运行的JavaScript连接到在另一个Docker容器中运行的MySQL数据库

时间:2019-06-18 18:52:07

标签: mysql node.js docker

我当前正在使用docker-compose和相应的docker-compose.yaml文件运行RocketChat和RocketBot的本地实例:

我使用标准的mysql模块,如下所示:

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

通过在包含MySQL服务器的容器上运行inspect命令来收集主机,用户,端口和密码。我可以运行MySQL并对其进行更改,甚至可以使用MySQL Workbench连接到它,因此MySQL确实可以正常工作。我收到此错误:

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)

我现在不知道如何继续,如何使用JavaScript从docker-compose服务的机器人连接到MySQL容器?

编辑:

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

1 个答案:

答案 0 :(得分:0)

通常,您可以使用容器名称作为主机名连接到另一个容器:

如果您有一个带有mysql的容器,则容器名称(在此示例中为'db')是访问mysql容器的主机名(也可以使用hostname: 'mysqlhostname'来指定其他名称):

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

在您的火箭聊天容器中,应为mysq根密码和数据库添加一些环境变量,以使其可用于您的容器

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

然后,使用主机名和环境变量创建连接:

var con = mysql.createConnection({
    host: 'db', // or process.env.MYSQL_HOSTNAME
    user: 'root',
    port: '3306',
    password: 'mypass', // or process.env.MYSQL_ROOT_PASSWORD
});