2

I'm trying to set up a docker-compose file with a container for asp.net-core, mysql database and phpmyadmin. There is no problem setting up my mysql-server, I can access it with phpmyadmin. Also my asp.net-core application is running correctly and I can access it in the browser.

However, I am not able to make a connection with my MySQL database. The application keeps returning:

Unable to connect to any of the specified MySQL hosts

The application is connecting through a connection string in appsettings.json.

{
  "ConnectionStrings": {
    "FlowerAPIConnection": "server=localhost;userid=user;password=user;database=bloemenapi_db;Convert Zero Datetime=True"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

My guess is that in the docker container the app and mysql are running can't seem to find each other on localhost. I tried using the ip-adress of the mysql container in the connectionstring, but that also did not work.

I'm using following docker-compose.yml and Dockerfile.

version: '3'

services:
  db:
    image: mysql
    restart: always
    container_name: flowerapi-db
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=user
      - MYSQL_ROOT_PASSWORD=user
      - MYSQL_DATABASE=bloemenapi_db
    ports:
      - "3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: flowerapi-pma
    ports:
      - "81:80"
    external_links:
      - db:mysql
    environment:
      PMA_HOST: "db"
      PMA_PORT: 3306
  web:
    image: flowerapi
    container_name: flowerapi-web
    build:
      context: ./FlowerAPI
      dockerfile: Dockerfile
    ports:
      - "5000:80"
    links:
      - db
    depends_on:
      - db

Dockerfile

FROM microsoft/aspnetcore:2.0
LABEL name "flower-api"
ARG source
WORKDIR /app
EXPOSE 5000/tcp
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "FlowerAPI.dll"]

Thanks for your help.

Patrick Steenks
  • 612
  • 1
  • 11
  • 24
  • *“I can access it with phpmyadmin”* – But *where* do you do that? Likely from within the same docker container the database runs in? That’s not the same way your web application will access it. It needs to be connected remotely. Docker containers are like separate computers. – poke Sep 12 '17 at 08:00
  • Yes, that was my guess. Phpmyadmin connects through localhost:81 to the MySQL database, so there the connection seems to exists. – Patrick Steenks Sep 12 '17 at 08:21
  • Did you solve this Patrick? I'm having a similar problem. I'm not using compose but can't get dotnet core to talk to mysql. I'm of course using the startup for mysql, verified mysql is up and can be reached etc. – agrath Feb 28 '19 at 06:06
  • No, unfortunately not. As a matter of fact, I gave up. :D – Patrick Steenks Mar 03 '19 at 18:51

4 Answers4

2

The container, by default, does not allow anyone to log into the server as root from outside the container. This prevents other containers (or the host too) from connecting to the db (using root credentials). You can use flag MYSQL_ROOT_HOST to pass the IP of the container or host which should be allowed to connect to the server with root credentials. Eg. To allow the host to connect, you would set MYSQL_ROOT_HOST="172.17.0.1".

Also I see you created only root user, but in connection string you use userid=user. You can use server=127.0.0.1 as host name.

kostia24
  • 848
  • 9
  • 11
1

Try this. Connection string is bit different then with MSSQL

"ConnectionStrings":{
        "FlowerAPIConnection":"Server=db;port=3306;Database=bloe‌menapi_db;User=user;Password=password;"
 }
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
1

MySql connector is needed for connecting .NET apps with MySql db. You'd need to install that connector in your asp.net core app container

zed101
  • 193
  • 1
  • 2
  • 10
0

localhost is inside each container, it won't work. Try to connect using db as hostname, and port 3306.

Lukas Kubis
  • 929
  • 5
  • 17
  • Ok, I've updated the connectionstring to `"FlowerAPIConnection": "server=db;port=3306;userid=user;password=user;database=bloemenapi_db;Convert Zero Datetime=True"`, but still no connection :( – Patrick Steenks Sep 12 '17 at 08:24