skip to Main Content

I am working under windows with Docker with a C# .NET 8.0 web app and a MySql database.
Everything looks fine but my webapp cannot connect to the MySql database in the db docker container.

I created a docker image with this Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

# Copy the entire solution and restore as distinct layers
COPY MyWebApp.sln .
COPY MyWebApp/MyWebApp.csproj MyWebApp/

RUN dotnet restore

# Copy everything else and build
COPY . .

RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/out .

ENTRYPOINT ["dotnet", "MyWebApp.dll"]

That works.
Then I create the image with:
docker build -t mywebapp-image .

In the image there is this ConnectionString:
Server=mywebapp_db;Database=myDatabase;Port=33063;User=root;Password=mySecretPassword;

Then I have this docker compose file, docker-compose.yml:

version: "3.9"
services:
    app:
        image: mywebapp-image
        ports:                                         
            - '8080:80'                    
        container_name: mywebapp_app   
        depends_on:
            - db             

    db:
        image: mysql/mysql-server:8.0
        cap_add:
            - SYS_NICE
        restart: always
        environment:
            - MYSQL_DATABASE=myDatabase
            - MYSQL_ROOT_PASSWORD=mySecretPassword
        ports:                                         
            - '33063:3306'  
        container_name: mywebapp_db

I run them using:
docker-compose up -d

The DB container works.

The App container exists with: Unhandled exception. MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. System.Net.Sockets.SocketException (111): Connection refused.

Where is the error?

  • I checked the server name (mywebapp_db) should be this one I think. Localhost or 127.0.0.1 does not work.
  • The port: 33063 looks correct to me. It is just 3306 which docker redirects to another number.
  • the user and password look identical to me.

I changed all the names for this example.

2

Answers


  1. Chosen as BEST ANSWER

    The answer of Hans (changing the port number in the connection string) was part of my solution (thank you). I finally got it working by adding MYSQL_USER and MYSQL_ROOT_HOST parameters to my docker-compose. I also changed the order of the docker-compose file so the MySql container start first. The app container may get an exception it could not connect the first time you run it. The second time it should work. The app was also running on a different port so I changed that one. Here is the final version of my docker-compose.yml:

    version: "3.9"
    services:
        mywebapp_db:
            image: mysql/mysql-server:8.0
            ports:
                - "3306:3306"
            cap_add:
                - SYS_NICE
            restart: always
            environment:
                MYSQL_DATABASE: myDatabase
                MYSQL_ROOT_PASSWORD: mySecretPassword
                MYSQL_USER: root
                MYSQL_ROOT_HOST: '%'
            container_name: mywebapp_db
        app:
            image: mywebapp-image
            ports:                                         
                - '8080:8080'                    
            container_name: mywebapp_app 
            depends_on:
                - mywebapp_db   
    

  2. Containers on the same Docker network communicate using the unmapped port numbers. The mapped ports are only for communicating with the containers from the host.

    So to access your database from the application container, you need to use port 3306 and your connection string should look like this

    Server=mywebapp_db;Database=myDatabase;Port=3306;User=root;Password=mySecretPassword;
    

    If you only need to access your database from the application, you don’t need to map the database container port at all.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search