使用docker-compose设置一个带有mysql

时间:2017-06-06 03:43:46

标签: mysql node.js docker

我正在尝试将使用mysql数据库的nodejs应用程序容器化。 nodejs app和mysql数据库各自驻留在它们自己的容器中。当我在没有Docker的情况下直接运行它时,这个堆栈可以工作。

这是我的docker-compose文件:

db:
  image: mysql:5.7
  ports:
    - "3306:3306"
  expose:
    - "3306"
  environment:
      MYSQL_ROOT_PASSWORD: secret-root
      MYSQL_DATABASE: dbname
      MYSQL_USER: web
      MYSQL_PASSWORD: web

app:
  build: .
  container_name: app
  volumes:
    - ".:/app"
  ports:
    - "3001:3000"
  links:
    - db
  environment:
    DB_HOST: db:3306
    DB_USER: web
    DB_PASS: web
    DB_PORT: "3306"

节点应用的Dockerfile

FROM node:6.10

RUN mkdir /app

WORKDIR /app

COPY package.json /app/package.json
RUN npm install
RUN npm i -g db-migrate

EXPOSE 3000

CMD tail -f /dev/null

通过执行docker-compose up,容器已成功启动。

更新1

然后,通过在应用容器内运行nodejs node_modules/nodemon/bin/nodemon -L ./bin/start.js来手动启动节点应用。

更新1结束

该应用程序正在侦听端口3000.我可以使用外部mysql客户端连接到数据库。

但是,每当应用程序尝试查询数据库时,我都会收到以下错误:

/app/node_modules/mysql/lib/protocol/Parser.js:79
        throw err; // Rethrow non-MySQL errors
        ^

TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
    at fromObject (buffer.js:262:9)
    at Function.Buffer.from (buffer.js:101:10)
    at new Buffer (buffer.js:80:17)
    at Object.Auth.token (/app/node_modules/mysql/lib/protocol/Auth.js:29:22)
    at Handshake._sendCredentials (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:78:29)
    at Handshake.HandshakeInitializationPacket (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:60:10)
    at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:75:12)
    at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)

我在这里缺少什么?

0 个答案:

没有答案