1

I have two containers defined in a docker-compose yaml file that need to talk to each other, but they can't.

version: "3.9"

networks:
  localdev:
    driver: 'bridge'

services:
  master-db:
    image: mysql:8.0
    container_name: master-db
    hostname: master-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "4000:3306"
    networks:
      - localdev

  page-store:
    hostname: page-store
    build:
      context: .
      dockerfile: Dockerfile.page_store
    container_name: page-store
    ports:
      - "2020:2020"
    networks:
      - localdev
    links:
      - master-db

In the page-store Python Flask microservice, I try to access the MySQL database by using its hostname of master-db, but the name cannot resolve.

Swatcat
  • 73
  • 6
  • 21
  • 57

1 Answers1

1

You should be able to connect each other using respective service names. master-db and page-store removing hostname

As per Official guide you may have to define master-db,page-store in container's /etc/hosts, if you want to use hostname: page-store etc.

Please refer this SO thread.

Also using --links may not be the best option.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94