Monday 5 June 2017

Using docker-compose to set up a nodejs app with mysql

I am trying to containerize a nodejs app which uses a mysql database. The nodejs app and the mysql database each resides in their own container. This stack works when I ran it directly without Docker.

Here is my docker-compose file:

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"

The Dockerfile for the node app:

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

By executing docker-compose up the containers have successfully started. The app is listening on port 3000 and I am able to connect to the db using a external mysql client.

However, whenever the app tries to query the database, I am getting the following error:

/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)

What am I missing here?



via Jason

No comments:

Post a Comment