3

I created a container with a mysql database and the manual connection with a .net application works very good. Now I would use a docker-compose to build the mysql container and my .net application image automatically.

My docker compose looks like that.

version: '3.1'

networks:
  overlay:
services:
  authentication_service:
    image: authentication_service:latest
    depends_on:
      - "authentication_service_db"
      - "adminer"
    build:
      context: .
      dockerfile: Dockerfile
    links:
      - authentication_service_db
    ports:
      - "5003:80"
    networks:
      - overlay
  authentication_service_db:
    image: mysql:8.0.3
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: authenticationdb
    ports:
      - 3305:3306
    restart: always
    volumes:
      - ./data-authentication:/var/lib/mysql
    networks:
      - overlay

When I start the application in development the connection string is printed and looks like that server=localhost;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret. This works.

When I run docker-compose up -d --build it changes: server=authentication_service_db;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret;

My connection test script:

private static void WaitForDbInit(string connectionString)
        {
            var connection = new MySqlConnection(connectionString);
            Console.WriteLine("Connecting with credentials: {0}", connectionString);

            var retries = 1;
            while (retries < 5)
            {
                try
                {
                    Console.WriteLine("Connecting to db. Trial: {0}", retries);
                    connection.Open();
                    connection.Close();
                    break;
                }
                catch (MySqlException)
                {
                    Thread.Sleep((int) Math.Pow(2, retries) * 1000);
                    retries++;
                }
            }
        }

The Log:

Connecting to db. Trial: 1
Connecting to db. Trial: 2
Connecting to db. Trial: 3
Connecting to db. Trial: 4
[...]
application startup exception: MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 299

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MysqlConnection": {
    "connectionString": "server=authentication_service_db;Port=3305;Database=authenticationdb;Uid=root;Pwd=secret;"
 }
}

So I don't know how debug or what I can do next. I don't get a connection when I use docker compose.

1 Answer 1

2

You mapped port 3306 to 3305 to your host, where you develop the application. So you can connect to db on port 3305. However, you are still using port 3305 inside the container. Because your service (authentication_service_db) is listening on port 3306, not 3305, your application having trouble to connect to database.

You must change connection string to use port 3306, instead of 3305.

Sign up to request clarification or add additional context in comments.

2 Comments

I will try this and will report. I completely do not understand it yet. My intention of the mapping is that I can run more than 1 MySQL-Database. There will be more services and every service should have his own database with his own port. How can every port listening to 3306? Could you explain it more in detail please.
In docker, there is no network connectivity between your containers and your host. So, in oder to connect to mysql service running on mydb:3306, you map it to your localhost:3305. When you want to add another mysql instance (eg: mydb2:3306) then you can map it to localhost:3304 then access via it. However, there is network connection between your containers. So application running inside containers should use container-ip/name:service-port pattern in order to connect to the service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.