I'm trying to containerize an Asp.Net Core application using Docker but on docker compose up aspnet app logs Unable to connect to any of the specified MySQL hosts and exits. I've read that Docker uses it's own internal network and provides DNS to connect to other container, so I use mysql service name in connection string, but it still doesnt connect.
It was working, when I only used docker to run mysql container, and exposed port 3306 to host, but for some reason it doesnt work now. What am I missing?
docker-compose
version: '3'
services:
mysql:
image: mysql:8
environment:
MYSQL_DATABASE: ctox_db
MYSQL_ROOT_PASSWORD: 019638
volumes:
- mysqldata:/var/lib/mysql
networks:
- ctox_net
ctoxweb:
build: .
depends_on:
- mysql
environment:
- "ConnectionStrings:AppContext=server=mysql; port=3306; database=ctox_db; user=root; password=019638"
links:
- mysql
networks:
- ctox_net
volumes:
mysqldata:
networks:
ctox_net:
driver: bridge
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// other services ...
var appContextConnection = Configuration.GetConnectionString("AppContext");
services.AddDbContext<AppDbContext>(options =>
options
.UseLazyLoadingProxies()
.UseMySql(appContextConnection, ServerVersion.AutoDetect(appContextConnection)));
}
PS I added environment variables to configuration, connection string is pulled correctly.