I have created a Docker image that runs a web server (based on Ocaml/Dream) on port 8080.
I run the container mapping host port 8084 to container port 8080 as follows :
docker run -p 8084:8080 <MyDockerImageID>
Now I curl for localhost:8084 from the host and I have the following error :
curl -v localhost:8084
* Host localhost:8084 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8084...
* Immediate connect fail for ::1: Cannot assign requested address
* Trying 127.0.0.1:8084...
* Connected to localhost (127.0.0.1) port 8084
> GET / HTTP/1.1
> Host: localhost:8084
> User-Agent: curl/8.5.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection
curl: (56) Recv failure: Connection reset by peer
Note that if I curl from inside the container, it works :
docker exec -ti <MycontainerID> curl localhost:8080
I also tried to run the server on port 80 inside the container and tried with different port mappings but the problem persists.
I finally tried the same port mapping with an Nginx container and it worked fine.
Any idea why is there this problem?
Update :
I just tried the solution described here by running the container on the host network
docker run --network host -d <MycontainerID>
and now it works.
However, from what I understand, this could rise security issues.