简体   繁体   中英

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

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

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM