what URL can docker container A use to access another docker container B (same dev machine, different projects)

jpw picture jpw · Mar 23, 2019 · Viewed 8.5k times · Source

Assuming we need to do this very rarely only at certain stages of development (to smoke test a few api calls), what is the simplest possible way to let a dockerized web service in project Bar access a dockerized web service in Project Foo?

On a development Mac, Docker Engine: 18.09.2, Compose: 1.23.2, we have Project Foo and Project Bar which each have their own docker-compose files, each with a web service and a database service.

Normally they run stand-alone, and are developed independently.

However Project Foo's web service hosts an API that only occasionally we want to access from Project Bar's web container

They are assigned to different host ports, docker ps shows Project Foo uses port 0.0.0.0:3000->3000/tcp (eg, we use localhost:3000 to access the web service from the Mac's browser. Project Bar uses port 0.0.0.0:3010->3010/tcp

docker inspect for Foo shows it's IP address is "172.18.0.3" (gateway "172.18.0.1") and for Bar shows it's IP address is "172.22.0.4" (gateway "172.22.0.1")

docker network ls shows they are both using the same "bridge" Driver.

Both projects are running on the Mac, 4 containers total, (web+db on Foo, and web+db on Bar)

If a program (Ruby) running on Bar needs to access a REST URL on Foo, what is the URL of "/api_test" on Foo?

From the Bar web container I've tried http://localhost:3000/api_test and http://127.0.0.1:3000/api_test (which is what we'd use from a web browser so didn't really expect that to work from container-to-container) and I've tried http://172.18.0.3:3000/api_test and http://172.18.0.3/api_test neither of which worked.

I see online references to setting up a link or a docker network, but all of the examples are for when using docker run instead of using docker-compose. I would expect that if you know the IP and port of each container's web server, it ought to be a matter of using the correct URL without any extra network setup?

Any help would be appreciated.

A manually-assigned static IP solution is preferred... Before Docker, we used Vagrant and that was simple, in each project's Vagrantfile we simply manually assigned them an IP on the same private subnet 192.168.50.50 and 192.168.50.51 and they "talked" to each other just fine, and we could simply 'code' those IPs into our development code. Docker seems to have an additional layer of abstraction that has me puzzled.

Answer

jpw picture jpw · Mar 23, 2019

I finally found a SO thread that mentioned for newer versions of Docker such as I am using the hostname host.docker.internal accesses the host machine, so one answer to my question is - since I already have assigned different ports to Foo and Bar - the Bar container can use the url http://host.docker.internal:3000/api_test

But it is still preferred to be able to assign static IPs to each container so I am leaving the question open.