.net - 无法连接到由 docker compose 启动的 .net 容器

标签 .net docker docker-compose

我用 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 , :80authentification_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/

相关文章:

c# - 从 C# 中的接口(interface)创建对象

c# - 用于 .NET 的快速且内存高效的 ASCII 字符串类

.net - 关于新的 EF5 自动编译查询功能

reactjs - 如何从日志文件而不是应用程序日志输出日志

node.js - 我可以使用 docker 容器来保存静态文件,以便其他 docker 容器可以访问它吗?

elasticsearch - 使用docker运行 Elasticsearch 的本地实例

docker - docker-compose无法识别sudoers容器文件

.net - 解决对高于目标的 .NET Framework 版本的依赖关系

java - 如何让 DropWizard JUnit 应用程序规则定义使用 docker 规则中的启动信息?

docker - 将Kubernetes Pod限制为一个进程