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:
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:
Maybe it could be, because for some reason env variable did not changed in env container?
UPDATE-1
Pasted the first answer code below, but nothing changed, still could connect with app only without docker
2
Answers
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.
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: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:In that case, you can connect either to
ws://localhost:3003
or towss://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:Docker-compose:
Now you can connect to
ws://localhost:3003
orwss://localhost:3004