I have a problem when trying to connect to dockerized mysql database from my dockerized microservice (on the same user defined bridge network).
This is my c# image:
FROM mcr.microsoft.com/dotnet/sdk:6.0 as build
COPY ./User /microservices/User
COPY ./Associated /microservices/Associated
WORKDIR /microservices/User/User.API
RUN dotnet restore
RUN dotnet build -o /app
RUN dotnet publish -o /publish -c Debug
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
COPY --from=build /publish /app
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:5114
EXPOSE 5114
ENTRYPOINT ["dotnet", "User.dll"]
# CMD ["./User"]
This is my compose file:
services:
user_api:
image: df_user_api:latest
container_name: user_api
build:
context: .
dockerfile: df_user_api
ports: [5114:5114]
depends_on: [mysql]
networks: [associated]
mysql:
image: mysql:latest
container_name: mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: roottooR1!
MYSQL_DATABASE: webshop2023
MYSQL_USER: test
MYSQL_PASSWORD: test123
MYSQL_LOG_CONSOLE: "1"
MYSQL_LOG_ERROR: "/var/log/mysql/error.log"
ports: [3306:3306]
networks: [associated]
volumes:
- "mysql-data:/var/lib/mysql"
- "mysql-log:/var/log/mysql"
- "mysql-conf:/etc/mysql/conf.d"
networks:
associated:
name: associated
volumes:
mysql-data:
driver: local
mysql-log:
driver: local
mysql-conf:
driver: local
error:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
user_api | Connection id "0HN006R0TSPN5", Request id "0HN006R0TSPN5:00000004": An unhandled exception was thrown by the application.
user_api | MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
Confusing part is that I can connect to dockerized mysql via mysql workbench from the host machine as: localhost:3036 (because mysql is exposed towards 0.0.0.0 by default)
Same is true for locally running c# microservice, I can connect to dockerized mysql also via localhost:3036.
These are my connection strings:
From localhost:
"server=localhost;port=3306;user=test;password=test123;database=webshop2023"
From within docker network:
"server=mysql;port=3306;user=test;password=test123;database=webshop2023"
I would be delighted if someone tells me what I am doing wrong. Sorry if the question is duplicate but I have read many articles aleready and I can't seem to figure out why it is not working.
It seems that everything was right, except that MySql framework that I was using was case sensitive on linux regarding the connection string parameters. Everyone that is developing on windows machine and then migrating your code to linux containers be exact with upper and lower case characters to not waste so much time like me.