1

Has anyone been able to bind to a [java.net InetSocketAddress ServerSocket] from inside docker? I have a simple NRepl process running in a docker container. But I can't connect to that NRepl process.

My docker-compose.yml looks something like this.

version: '3'
services:
  foo:
    ports:
      - ${NREPL_PORT:-7001}:7001

In the dockerized app, something like below is happening to bind to a host/port.

InetSocketAddress ia = new InetSocketAddress("0.0.0.0" 7001);

ServerSocket s = new ServerSocket();
s.setReuseAddress(true);
s.bind(ia);

But I consistently get an error when trying to connect from outside of docker.

λ lein repl :connect localhost:7001
Connecting to nREPL at localhost:7001
SocketException The transport's socket appears to have lost its connection to the nREPL server
        nrepl.transport/bencode/fn--9182/fn--9183 (transport.clj:108)
        nrepl.transport/bencode/fn--9182 (transport.clj:108)
        nrepl.transport/fn-transport/fn--9150 (transport.clj:55)
        clojure.core/binding-conveyor-fn/fn--5739 (core.clj:2030)
        java.util.concurrent.FutureTask.run (FutureTask.java:266)
        java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617)
        java.lang.Thread.run (Thread.java:745)

I've tried to listen on these addresses (listening to localhost wouldn't be available from the docker host), but I get the same error every time.

  "0.0.0.0"
  "::"

And on running, I check and have the expected ports open.

λ docker ps
CONTAINER ID    IMAGE      COMMAND       CREATED         STATUS         PORTS                     NAMES
4d39d81fba69    api_api    "lein run"    5 minutes ago   Up 5 minutes   0.0.0.0:7001->7001/tcp    api_api_1

These are similar questions (a, b) that didn't quite help in my case.

Nutritioustim
  • 2,686
  • 4
  • 32
  • 57
  • 1
    You describe what's happening inside the Docker container as if it's Java code -- is it? nrepl can only connect to an nrepl server, not an arbitrary socket server. – Sean Corfield Jan 16 '20 at 04:22
  • 1
    @SeanCorfield So I got this working. There was something weird going on with the routing tables between my host and docker. Everything's good now. Thanks for following up. – Nutritioustim Jan 17 '20 at 19:22
  • 1
    @Nutritioustim how did you solve this? I have a similar issue, and have had weird docker networking issues before. – Mattias Arro May 03 '21 at 12:27
  • 1
    @MattiasArro The following may be helpful to you although may be a different scenario. https://www.fbrs.io/nrepl-docker/ – Daniel Gerson May 08 '22 at 14:56

0 Answers0