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.