Search code examples
dockervaporvapor-fluent

Fatal error: Error raised at top level: NIO.SocketAddressError.unknown(


Hi everyone i am new in vapor and i trying to connect a Vapor application that is in a docker container to a MySQL database that is in another docker container using fluent but I get the following error

unknown(host: "mysql_host", port: 3306)
Swift/ErrorType.swift:200: Fatal error: Error raised at top level: 
NIO.SocketAddressError.unknown(host: "mysql_host", port: 3306)

this is my configuration on configure.swift:

app.databases.use(.mysql(hostname:"mysql_net", 
username:"sergio", 
password:"holaMundo", 
database:"vapor",
tlsConfiguration: .forClient(certificateVerification: .none)), as: .mysql)

and this is my docker-compose:

version: '3.7' 
services:
mysql:
  build:
    context: .
    dockerfile: Dockerfile
  container_name: bd
  restart: always
  ports:
   - 3306:3306
  environment: 
    MYSQL_ROOT_PASSWORD: "holaMundo"
    MYSQL_DATABASE: "vapor"
    MYSQL_USER: "sergio"
    MYSQL_PASSWORD: "holaMundo"
  volumes:
    - ./schemas:/var/lib/mysql:rw
networks:
  mysql_network:
    aliases:
      - mysql_host
volumes:
  schemas: {}
networks:
  mysql_network:
    name: mysql_net
    driver: bridge

Solution

  • The hostname of your databased is aliased as mysql_host, mysql_net. Is the name of the network.

    Note you can remove all of the network code - services are networked together by default in compose. You can just use mysql as the hostname