title: TCP/Telnet from inside docker container
body: <p>I have a 'mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim' Docker container, with 2 things on it:
  -a custom SDK running on it
  -a dotnet5 application that connects to the SDK with TCP</p>
  <p>If I connect the the Docker's bash, I can use <code>telnet localhost 54321</code> to connect to my SDK successfully</p>
  <p>If I run the Windows SDK version on my development computer (Windows), and I run my application IIS Express instead of Docker, I can successfully connect with a telnet library (host 'localhost', port '54321'), this works</p>
  <p>However, I want to run both the SDK and my dotnet application in a Docker container, and when I try to connect from inside the container (the same thing that works on the IIS hosted version), this does not work. By running 'telnet localhost 54321' from the docker container commandline I can confirm that the SDK is running. What am I doing wrong?</p>
  <p>Dockerfile:</p>
  <pre><code>FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
  
  RUN apt-get update &amp;&amp; apt-get install -y telnet
  RUN apt-get update &amp;&amp; apt-get install -y libssl1.1
  RUN apt-get update &amp;&amp; apt-get install -y libpulse0
  RUN apt-get update &amp;&amp; apt-get install -y libasound2
  RUN apt-get update &amp;&amp; apt-get install -y libicu63
  RUN apt-get update &amp;&amp; apt-get install -y libpcre2-16-0
  RUN apt-get update &amp;&amp; apt-get install -y libdouble-conversion1
  RUN apt-get update &amp;&amp; apt-get install -y libglib2.0-0
  
  
  RUN mkdir /sdk
  
  COPY [&quot;Server/Sdk/SomeSDK*&quot;, &quot;sdk/&quot;]
  
  WORKDIR /app
  EXPOSE 80
  EXPOSE 443
  
  FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
  WORKDIR /src
  COPY [&quot;Server/MyProject.Server.csproj&quot;, &quot;Server/&quot;]
  COPY [&quot;Shared/MyProject.Shared.csproj&quot;, &quot;Shared/&quot;]
  COPY [&quot;Client/MyProject.Client.csproj&quot;, &quot;Client/&quot;]
  RUN dotnet restore &quot;Server/MyProject.Server.csproj&quot;
  COPY . .
  WORKDIR &quot;/src/Server&quot;
  RUN dotnet build &quot;MyProject.Server.csproj&quot; -c Release -o /app/build
  
  FROM build AS publish
  RUN dotnet publish &quot;MyProject.Server.csproj&quot; -c Release -o /app/publish
  
  FROM base AS final
  
  WORKDIR /app
  COPY --from=publish /app/publish .
  ENTRYPOINT [&quot;dotnet&quot;, &quot;MyProject.Server.dll&quot;]
  </code></pre>
  <p>Code (working on IIS, when SDK is running on Windows, but not when both SDK and code are running inside the same Docker container):</p>
  <pre><code>var telnetClient = new TelnetClient(&quot;localhost&quot;, 54321, TimeSpan.FromSeconds(1), new CancellationToken());
  await telnetClient.Connect();
  Thread.Sleep(2000);
  await telnetClient.Send(&quot;init&quot;);
  </code></pre>
  <p>Command line (working BOTH from windows CLI, and from Docker bash (so also when the code is not working, this is working):</p>
  <pre><code>$telnet localhost 54321
  $init
  </code></pre>
  <p>The issue might be (but I'm not sure, as I've received this result from using direct command line '$telnet localhost 54321' from within dotnet: 'telnet: connection refused by remote host'</p>