我有一个项目,包括.Net和Java的容器化docker应用程序。从我的compose文件中可以看出,每个应用程序都有自己的数据库。
我的问题是,当我使用compose运行所有容器时,我的应用程序无法访问数据库。但是,当我将数据库作为容器运行,并且我的PC(dotnet / java)上各自本地运行时安装的应用程序时,我的应用程序连接到数据库就好了。我怀疑我的compose文件中有一些网络相关的配置我不知道,这导致了麻烦。有没有人对我做错了什么有任何建议?
错误消息
Java app
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
.Net app
An unhandled exception occurred while processing the request.
ExtendedSocketException: Connection refused 127.0.0.1:1433
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
AggregateException: One or more errors occurred. (Connection refused 127.0.0.1:1433)
System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)
SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
搬运工-compose.yml
version: '3'
services:
identity.api:
image: unify/identity.api:${TAG-latest}
build:
context: ./src/Services/IdentityService/src
env_file:
- 'aspnetcore.env'
ports:
- 5000:80
course.api:
image: unify/course.api:${TAG-latest}
build:
context: ./src/Services/CourseService/src
env_file:
- 'aspnetcore.env'
ports:
- 5001:80
depends_on:
- "course.data"
- "identity.api"
course.data:
image: microsoft/mssql-server-linux:latest
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=yourStrong(!)Password
volumes:
- course.data.volume:/var/opt/mssql
message.api:
image: unify/message.api:${TAG-latest}
build:
context: ./src/Services/MessageService
ports:
- 8080:8080
depends_on:
- "message.data"
message.data:
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
volumes:
- message.data.volume:/var/lib/mysql
volumes:
course.data.volume:
message.data.volume:
答案 0 :(得分:0)
ExtendedSocketException: Connection refused 127.0.0.1:1433
loopback接口在docker容器中命名空间,每个容器都会获得它自己的私有环回,它与主机和其他容器分开。要执行容器到容器网络:
这意味着您需要重新配置您的应用以连接到" course.data:1433"而不是" 127.0.0.1:1433"。