I am using MySQL with .net5 web API in docker. My docker configuration is Window10 Home with WSL2.
My Docker-compose and compose.override file like below
docker-compose.yml
version: '3.4'
services:
userdb:
image: mysql
portainer:
image: portainer/portainer-ce
usermanagement.api:
image: ${DOCKER_REGISTRY-}usermanagementapi
build:
context: .
dockerfile: Services/User/User.API/Dockerfile
volumes:
mysql_data:
portainer_data:
docker-compose.override.yml
version: '3.4'
services:
userdb:
container_name: userdb
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=user
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
portainer:
container_name: portainer
restart: always
ports:
- "9000:9000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
usermanagement.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- "ConnectionStrings:DefaultConnection=server=userdb;port=3306;database=user;userid=root;pwd=root;"
depends_on:
- userdb
ports:
- "8001:80"
Whenever I build or up my docker-compose file the first time (fresh start or cleaning old volume then D-C up). Then it throws error MySql connection error MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
Then I have to manually stop and then start my API container. then the connection workss fine.
Am I doing something wrong or any solution for this issue Please share?
The docket compose docs have some answers.
Either change your app to retry the connection or use a wrapper script that waits for the db port to be ready
I was doing a DB migration in the program.cs file. and MySQL server starts slower then .Net5 API server.
To resolve this issue As per @Rosco Answer I have to retry my migration for an exception.
For retrying migration example: