我用 mysql 数据库启动了一个 .net 服务,所有容器都在运行
109eeaf260e8 authentication_service:latest "dotnet out/Authenti…" 9 seconds ago Up 7 seconds 0.0.0.0:5003->80/tcp authenticationservice_authentication_service_1
5da4ab86b093 mysql:8.0.3 "docker-entrypoint.s…" 10 seconds ago Up 8 seconds 0.0.0.0:3305->3306/tcp authenticationservice_authentication_service_d
现在我通过 postman 向
http://192.168.0.4:5003/api/authentication/5
发送 HTTP 请求。我试过 :5000
, :80
或 authentification_serivce:PORT
也是。但我总是收到超时或未找到。当我通过 IDE 启动它并调用
http://localhost:5000/api/authentication/5
我收到预期值。我尝试使用
EXPOSE 80
在我的Dockerfile
但没有成功。我的 docker 撰写:
version: '3.1'
networks:
overlay:
services:
authentication_service:
image: authentication_service:latest
depends_on:
- "authentication_service_db"
- "adminer"
build:
context: .
dockerfile: Dockerfile
links:
- authentication_service_db
ports:
- 5003:80
networks:
- overlay
authentication_service_db:
image: mysql:8.0.3
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: authenticationdb
ports:
- 3305:3306
restart: always
volumes:
- ./data-authentication:/var/lib/mysql
networks:
- overlay
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
- overlay
我的容器是活的(和听的)
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
所以我很困惑。如何在我的服务中连接到 .net 应用程序?似乎我的端口映射错误,或者我忘记了 docker-compose 或 .net 中的基本配置?
(如果您需要来自项目或 Dockerfile 的更多配置,请发表评论)
最佳答案
您正在定义 authentication_service
带端口映射
ports:
- 5003:80
在你的 docker-compose.yml 中。这意味着您的 docker 主机上的端口 5003 上的请求将被转发到您的 authentication_service-container 的端口 80。
因此(如果 authentication_service 的容器确实在端口 80 上监听)您必须在 docker 主机上触发的 URL 是:
http://localhost:5003/api/authentication/5
关于.net - 无法连接到由 docker compose 启动的 .net 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135903/