Docker Windows container can't resolve host.docker.internal
Asked Answered
H

1

12

My windows docker container cannot resolve host.docker.internal, but it's working with Linux containers. I would prefer to stay with Linux containers, but I am having trouble with an old .Net framework 4.7.2 application.

My application connects to RabbitMQ on the local machine. This works as expected in the Linux containers, but with the windows container it throws this error:

System.Net.Sockets.SocketException: No such host is known

I've tried pinging host.docker.internal.

C:\app>ping host.docker.internal Ping request could not find host host.docker.internal. Please check the name and try again.

Here is my dockerFile:

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019
ARG source
WORKDIR /app
COPY ${source:-SampleApp/obj/Docker/publish} .
ENTRYPOINT ["C:\\app\\SampleApp.exe"]

I am using docker version 20.10.2, and docker desktop version 3.1.0.
I'm running on Windows 10, version 10.0.18363.

Humidify answered 25/2, 2021 at 16:54 Comment(1)
Were you able to fix it ? Me too facing the same issue. Kindly answer your own question and let us know how the issue was fixedSwain
S
0

I fixed this issue in a roundabout way. In my docker-compose.yml file, I created a network and bound a static IP as the gateway ip like below

version: '3.4'

services:
  service-name:
    networks:
        local_net:
            ipv4_address: 172.18.0.2
networks:
  local_net:
    ipam:
        config:
           -
            subnet: 172.18.0.0/25
            gateway: 172.18.0.1

After this, in my docker file, I added the below command

RUN $file = $Env:windir+'\System32\drivers\etc\hosts'; '172.18.0.1 host.docker.internal' | Add-Content -PassThru $file;

And it is working like a charm for me now.

Sot answered 26/10, 2023 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.