skip to Main Content

Created an app, with websocket server, which should return some answer. I could establish a ws connection to a ws server when app is not containerized in docker.

But when I containerize it in a docker, I could not establish the ws connection:

docker-compose.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: MyAppDockerfile

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"]

My websocket server is listening on ws://127.0.0.1:3003.

I exposed port 3003 in Dockerfile and added it it docker-compose, but I’m receiving such answer when I’m trying to establish ws connection to app in docker container via postman:

enter image description here

May be I’ve missed something in docker settings?

Any help would be much appreciated. Thanks.

UPDATE

Removed from Dockerfile
ENV ASPNETCORE_URLS=https://+:3003

And docker-compose setting is in old variant

environment:
ASPNETCORE_URLS : "https://+:3003"

But nothing changed, when I replaced connection to wss:

after changes

Maybe it could be, because for some reason env variable did not changed in env container?

Env variables screenshot

UPDATE-1

Pasted the first answer code below, but nothing changed, still could connect with app only without docker

2

Answers


  1. Chosen as BEST ANSWER

    As a result: The reason was, that I've used the HttpListener class on a localhost IP. First of all I changed listen address of WebSocket Server

    AND added a string into a docker file, after default EXPOSE values:

    USER ContainerAdministrator

    After that I've been able to connect to port 56222, that you could establish in docker compose or it will be established by docker itslef(without docker compose):

    enter image description here

    on ANY ip with that port.


  2. You are trying to connect to the HTTP endpoint ws://localhost:3003, but your server only listens to the HTTPS endpoints. Change server to listen to HTTP endpoints:

    ASPNETCORE_URLS : "http://+:3003"
    

    If you want to use secure WebSockets then keep your server settings but connect to wss://localhost:3003. You also can listen to both schemas:

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

    In that case, you can connect either to ws://localhost:3003 or to wss://localhost:3004 (don’t forget to add port 3004 binding).


    NOTE: You don’t need to bake ASPNETCORE_URLS into your image and expose custom ports. Let the server use default ports (80 and 443) and allow users to bind them to any port on the host:

    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"]
    

    Docker-compose:

     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: MyAppDockerfile`
    

    Now you can connect to ws://localhost:3003 or wss://localhost:3004

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search