I want to connect my application with MySQL by Docker-compose. But, when I go to https://localhost:5001/api/Contacts (my Api), it fails to connect to database (Error: ERR_SSL_PROTOCOL_ERROR).
Here is my Docker-compose:
services:
mysql:
image: mysql
restart: always
volumes:
- ./setup.sql:/docker-entrypoint-initdb.d/setup.sql
- dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: admin
ports:
- 3307:3306
webapi:
image: webapi
build: .
restart: always
depends_on:
- mysql
ports:
- 5001:80
volumes:
dbdata:
Here is my Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DotnetCoreTrainingContext>(options =>
.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
new MySqlServerVersion(new Version())));
services.AddControllers();
}
Here is my appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "server=mysql;user=root;password=admin;port=3307;database=DotnetCoreTraining;sslmode=none;"
}
How can I set server=mysql (name of MySQL image on Docker in my computer) in ConnectionStrings?
CodePudding user response:
When you're connecting from another container on the bridge network created by docker-compose, you connect using the port the container is listening on. The mapped port is used when you connect from the host machine.
In your case that means that you need to connect on port 3306 and your connection string should be
server=mysql;user=root;password=admin;port=3306;database=DotnetCoreTraining;sslmode=none;
