Search code examples
mysqldockerdocker-compose

Not able to connect to mysql server at port 3306 from docker containers of microservices


I have created a docker-comose file with all mandatory info regarding db and microservices and after running docker compose up command, even my db images are running successfully, my microsevice instance was not able to connect with them and returning Communication Link error, I tried many ways but error remains same.

Please suggest the better way to do the same from below code:

common.config.yml
services:
  network-deploy-service:
    networks:
      - xyz

  microservice-db-config:
    extends:
      service: network-deploy-service
    image: mysql
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      timeout: 10s
      retries: 10
      interval: 10s
      start_period: 10s
    environment:
      MYSQL_ROOT_PASSWORD: root

  microservice-base-config:
    extends:
      service: network-deploy-service
    deploy:
      resources:
        limits:
          memory: 700m

docker-compose.yml
services:
  accountsdb:
    container_name: accountsdb
    ports:
      - 3307:3306
    environment:
      MYSQL_DATABASE: accountsdb
    extends:
      file: common-config.yml
      service: microservice-db-config

  loansdb:
    container_name: loansdb
    ports:
      - 3308:3306
    environment:
      MYSQL_DATABASE: loansdb
    extends:
      file: common-config.yml
      service: microservice-db-config

  cardsdb:
    container_name: cardsdb
    ports:
      - 3309:3306
    environment:
      MYSQL_DATABASE: cardsdb
    extends:
      file: common-config.yml
      service: microservice-db-config

  accounts:
    image: "xyz/accounts"
    container_name: accounts-ms
    ports:
      - "8080:8080"
    environment:
      SPRING_APPLICATION_NAME: "accounts"
      SPRING_DATASOURCE_URL: "jdbc:mysql://accountsdb:3306/accountsdb"
    depends_on:
      accountsdb:
        condition: service_healthy

  loans:
    image: "xyz/loans"
    container_name: loans-ms
    ports:
      - "8090:8090"
    environment:
      SPRING_APPLICATION_NAME: "loans"
      SPRING_DATASOURCE_URL: "jdbc:mysql://loansdb:3306/loansdb"
    depends_on:
      loansdb:
        condition: service_healthy

  cards:
    image: "xyz/cards"
    container_name: cards-ms
    ports:
      - "9000:9000"
    environment:
      SPRING_APPLICATION_NAME: "cards"
      SPRING_DATASOURCE_URL: "jdbc:mysql://cardsdb:3306/cardsdb"
    depends_on:
      cardsdb:
        condition: service_healthy

networks:
  xyz:
    driver: "bridge"

Solution

  • Your databases are on the xyz network, but your application containers aren't. They're on the default bridge network. That's why they can't talk to each other.

    You probably meant to let your application containers extend microservice-base-config but forgot? Doing that will put them on the xyz network and will let them talk to the database containers.

    That being said, I don't really see any benefit of defining your own network over using the default bridge network that compose creates, so I'd just delete the xyz network and use the default network for all the containers.