Search code examples
docker-composevaporfluent-mysql

FluentMySQL migration fails when building with docker-compose but not when building from Xcode


I'm getting the following crash when using docker-compose with Vapor & FluentMySQL.

api_1  | [ INFO ] Migrating 'mysql' database (/app/.build/checkouts/fluent/Sources/Fluent/Migration/MigrationConfig.swift:69)
    api_1  | Fatal error: Error raised at top level: NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "db", port: 3309, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIO.SingleConnectionFailure(target: [IPv4]db/172.27.0.2:3309, error: connection reset (error set): Connection refused (errno: 111))])): file /home/buildnode/jenkins/workspace/oss-swift-5.1-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200

Migration fails when using:

migrations.add(model: Model.self, database: .mysql)

If I remove that so no Models are migrated the app builds without errors and I am able to access it at http://localhost/.

My docker-compose.yml looks like this:

version: "3.7"
services:
  api:
    image: vaporapiimage
    ports:
      - 80:8080
    environment:
      MYSQL_HOST: db
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: dbname
      SLEEP_LENGTH: 7
      MYSQL_PORT: 3309
    depends_on:
        - db
  db:
    image: mysql:8.0.1
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dbname
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3309:3306"

It builds and runs as expected locally from Xcode (as in without Docker).

If the api service is commented out then the db service will run on its own and create the database on first run.

Any help would be greatly appreciated.

UPDATE: Replacing the api with Adminer results in being able to access the db through Adminer in the browser. So the db service is accessible and it seems as if the issue is with Vapor/Fluent (or rather my use of them):

  admin:
    image: adminer
    ports:
      - 8080:8080

Solution

  • The problem was that I had mapped the port for db to 3309 so that it didn't conflict with the host MYSQL but had also set the ENV 'MYSQL_PORT' to 3309 when that should have been kept as the default 3306 because that was for communication between containers.