无法连接到Docker-compose内部的WebSocket服务器应用程序

o4hqfura  于 2023-01-29  发布在  Docker
关注(0)|答案(1)|浏览(325)

创建了一个应用程序,与websocket服务器,这应该返回一些答案。我可以建立一个ws连接到一个ws服务器时,应用程序没有容器化在docker。
但是当我把它装进码头时,我无法建立WS连接:

    • 停靠-编写. yml**
services:
  myapp
    container_name: MyApp
    hostname: MyApp-host
    image: ${DOCKER_REGISTRY-}myapp
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    ports:
      - "3003:3003"
    environment:
      ASPNETCORE_URLS : "https://+:3003"
    build:
      context: .
      dockerfile: MyApp\Dockerfile
    • 停靠文件**
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

ENV ASPNETCORE_URLS=https://+:3003
EXPOSE 3003

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["AppFolder/MyApp.csproj", "AppFolder/"]
RUN dotnet restore "AppFolder/MyApp.csproj"
COPY . .
WORKDIR "/src/AppFolder"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

我的网络套接字服务器正在监听ws://127.0.0.1:3003。
我暴露了端口3003在Dockerfile中,并将其添加到它的docker-compose,但我收到这样的答案,当我试图通过 Postman 建立WS连接到Docker容器中的应用程序:

可能是我错过了什么在码头设置?
任何帮助都将不胜感激。谢谢。

    • 更新**

从文档环境ASPNETCORE_URLS = https://+:3003中删除
和docker-compose设置是旧的变体
环境:ASPNETCORE_网址:"网址:https://+3003"
但什么都没有改变,当我更换连接到wss:
after changes
也许是这样,因为某些原因env变量在env容器中没有改变?
Env variables screenshot

    • 更新-1**

粘贴了下面的第一个答案代码,但没有任何变化,仍然可以连接应用程序,只有没有Docker

7cwmlq89

7cwmlq891#

您正在尝试连接到HTTP终结点ws://localhost:3003,但您的服务器仅侦听HTTPS终结点。请更改服务器以侦听HTTP终结点:

ASPNETCORE_URLS : "http://+:3003"

如果你想使用安全的WebSockets,请保留你的服务器设置,但连接到wss://localhost:3003。你也可以监听两个模式:

ASPNETCORE_URLS : "http://+:3003;https://+:3004"

在这种情况下,您可以连接到ws://localhost:3003wss://localhost:3004(不要忘记添加端口3004绑定)。
注意:你不需要把ASPNETCORE_URLS绑定到你的映像中,也不需要暴露自定义端口。让服务器使用默认端口(80和443),并允许用户将它们绑定到主机上的任何端口:

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["AppFolder/MyApp.csproj", "AppFolder/"]
RUN dotnet restore "AppFolder/MyApp.csproj"
COPY . .
WORKDIR "/src/AppFolder"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

坞站-组合:

services:
  myapp
    container_name: MyApp
    hostname: MyApp-host
    image: ${DOCKER_REGISTRY-}myapp
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    ports:
      - "3003:80"
      - "3004:443"
    build:
      context: .
      dockerfile: MyApp\Dockerfile`

现在,您可以连接到ws://localhost:3003wss://localhost:3004

相关问题