How to connect to an external service and port from a Docker container

Viewed 117

I have some legacy code that uses old versions of Ruby and Rails and Elasticsearch. This code is old enough that I can't get the version of Elasticsearch that is defined in docker-compose.yml to run on my M1 MacBook Pro so I decided to download the old version of it and run it along side my Docker containers.

However, in doing so, my other Rails container now doesn't know to access Elasticsearch. Is there some way to structure my docker-compose.yml such that my Rails container knows where and on what port Elasticsearch is listening?

1 Answers

Sure, that's possible.
You never asked for it, but I suppose you also want to know how to achieve that ;-)

In the Dokumentation it's described quite well, this is the essential page and this the important section:

enter image description here

Instead you could also use a socket connection which is can be achieved with the Daemon dockerd:

sudo dockerd -H unix:///var/run/docker.sock

It's also possible to expose several connections:

sudo dockerd -H unix:///var/run/docker.sock -H tcp://192.168.59.106 -H tcp://10.10.10.2

Other containers can use then the exposed container just simple like this:

docker -H unix:///var/run/docker.sock

Options for the docker file
Above options aren't for the docker file but there exist helpul settings too, likely you'd need only the option expose

EXPOSE 80/tcp  
EXPOSE 80/udp

Nevertheless on that page you find many more options that could be helpful, like ENV perhaps.

Take care that for the compose file the definition of expose is a bit different than for the docker file.

Related