Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 430
jpw Avatar asked Mar 23 '19 07:03

jpw


People also ask

What is the URL for docker?

The official docker hub website has been moved from https://hub.docker.com/ to https://registry.hub.docker.com.

How do I find container URL?

On browser type http://localhost:54529/your_application_page. if you are running application locally, then this is the port which needs to be changed in browser to access container application.

How multiple docker containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.


Video Answer


1 Answers

TLDR: Use http://host.docker.internal

From https://docs.docker.com/docker-for-mac/networking/,

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.

I tried and this works for windows as well.

For example, if you are running a python app/microservice in a container on port 5000, you may use this to connect to it:

requests.get('http://host.docker.internal:5000')
like image 195
joe Avatar answered Oct 03 '22 05:10

joe