How do I open ports on a docker image?

I am using a docker file as shown here, in my project :

I start the container with:

docker-compose up app

But cannot connect to the socket. Can’t see what I am doing wrong.

The ports are not accessible either on my local system or when I put the docker container on a server.

After docker-compose up app I see the following:

 docker container ls
 CONTAINER ID   IMAGE                            COMMAND                  CREATED          STATUS          PORTS                                                                                NAMES
 886341940c74   embedded-repl_app                "entrypoint java -ja…"   30 minutes ago   Up 30 minutes   0.0.0.0:2001->54654/tcp, :::2001->54654/tcp                                          embedded-repl_app_1

But then when I try to telnet:

telnet localhost 2001
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Your container is working as expected. I think your testing is the problem. I don’t know what that server does exactly but it most definitely doesn’t speak telnet.

The connection happened… the server closed it. Why not try with an official client for that server?

Thanks @beardstack . Well I have tried it with the official client for the server. It is a Clojure nrepl server to which a repl (read evaluate print loop) client should be able to connect. Indeed when the server is not in a docker container the software connects fine but when in a docker container I cannot connect.

I just used telnet to see any messages exchanged between the server and the client.

What is the bind address? I don’t know nREPL, but according to the documentation, the default bind address is 127.0.0.1. It means the server is listening on the loopback IP address so you can connect it from the same machine (inside the container) but not from the host. The server inside the container needs to listen on an IP of a Docker network or all of the available IP addresses like 0.0.0.0

It is true even if you use the -p parameter or ports in Docker Compose to forward ports since it will be forwarded to the container’s IP address.

https://nrepl.org/nrepl/usage/server.html#starting-a-server

-b/–bind ADDR Bind address, by default “127.0.0.1”.

Thanks so much @rimelek ! That was indeed the problem. The nrepl server was expecting connections at 127.0.0.1, I didn’t realise this was the default and didn’t know that if a docker container is running on my own machine that it might appear within the container as if connections are coming from another ip.

Ah, so what I was hoping to do is use this kind of set up on a remote server, so I can embed a REPL in code running remotely.

I was going to ssh in to the server and local forward the port to myself. I want the nrepl to be available if I ssh in but not available to connect to otherwise. Is this possible? Ie. can I tell the container or the server to allow local connections from the ssh daemon but not from an external IP address?

If you don’t want to allow external connections, forward the port only from a local IP address. nreptl still should listen on every IP addresses but it only means it listens on a Docker network which is not accessible from outside unless you forward a port to it. By default (as you can see in the output of docker ps) docker will forward the port from all of the IP addresses available on the host, including the public IP address. You could use a firewall software on the host to deny any external request, or just change the port forward

    ports:
      - 127.0.0.1:2001:54654

Wonderful! Thanks for the help!!