Docker-Compose Unable to connect to any of the specified MySQL hosts

女生的网名这么多〃 提交于 2021-01-29 21:46:28

问题


i have an existing Project ( API - portal - Mysql ) i have used Docker-compose without dockerfile i publish the API - Portal and put them all in folder and then Docker-compose up

i can reach the api by getting the local values in it but if i tried to reach mysql trough the API using postman its not working even when i open the frontend website

ConnectionString:

    "ConnectionString": "server=xmysql;port=4406;Database=sbs_hani;User ID=hani;Password=123456; persistsecurityinfo=True;Charset=utf8; TreatTinyAsBoolean=false;"

This is my docker-compose file :

version: '3'

services:

  xmysql:
    container_name: xmysql
    hostname: xmysql
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_DATABASE: "sbs_hani"
      MYSQL_USER: "hani"
      MYSQL_PASSWORD: "123456"
    ports:
     - "3306:4406"
    networks:
      - xnetwork
    volumes:
      - data-volume:/var/lib/mysql
      - ./hanimysql/sbs_hani.sql:/docker-entrypoint-initdb.d/sbs_hani.sql

  xapi:
    container_name: xapi
    hostname: xapi
    image: microsoft/dotnet:latest
    # restart: always
    tty: true
    command: ["dotnet", "/var/lib/volhaniapi/hani.APIs.dll"]
    ports:
      - "8081:80"
      - "8444:443"
    networks: 
      - xnetwork
    links:
      - xmysql:xmysql
    depends_on:
      - xmysql
    volumes:
      - ./haniapi/:/var/lib/volhaniapi/


  xportal:
    container_name: xportal
    hostname: xportal
    image: microsoft/dotnet:latest
    # restart: always
    tty: true
    command: ["dotnet", "/var/lib/volhaniportal/hani.Portal.dll"]
    ports:
      - "8083:80"
      - "8446:443"
    networks: 
      - xnetwork
    links:
      - xmysql:xmysql
    depends_on:
      - xmysql
    volumes:
      - ./haniportal/:/var/lib/volhaniportal/

  xfront:
    container_name: xfront
    hostname: xfront
    image: nginx:stable-alpine
    # restart: always
    ports:
      - "8082:80"
      - "4445:443"
    networks: 
      - xnetwork
    links:
      - xapi:xapi
    depends_on:
      - xapi
    volumes:
      - ./hanifront/:/usr/share/nginx/html


volumes:
  data-volume: {}
  # xvolmysql:
  #   driver: "local"
  # xvolmongo:
  #   driver: "local"
  # xvolrabbitmq:
  #   driver: "local"
  # xvolstarapi:
  #   driver: "local"

networks:
  xnetwork:
    driver: bridge

回答1:


When you make a connection from one Docker container to another, you always connect to the port the service inside the container is actually listening on. Any ports: mappings are ignored (and in fact you don't need ports: if you don't want the service to be accessible from outside Docker container space).

In your example, you need to change the port number in the connection string to the default MySQL port 3306.

(Consider removing all of the container_name:, hostname:, networks:, and links: blocks in the file. You should have an equivalent container stack with the same functionality; the two most observable differences are that if you directly use docker commands then the container names will be prefixed with the directory names, and the Docker-internal network will be named default. You can still use the service block names like xmysql as host names.)



来源:https://stackoverflow.com/questions/57851823/docker-compose-unable-to-connect-to-any-of-the-specified-mysql-hosts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!