Will
Will

Reputation: 8641

Two independent Docker containers communicating via REST

I've got two monoliths coming up via make commands, lots of dependencies and takes a very long time. One of the monolith's APIs wishes to call a REST endpoint within the other monolith. Both are running on my localhost which I can ping from localhost. However when I try to ping from within the monolith times outs.

Forgive my ignorance but I do not do a lot of docker or networking, but what information must I pass into the CALLER API container from the RECEIVERS API in order for them to communicate.

They both come up via two different docker-compose.yaml file of defined services such:

services:
  pims-api.lh.local:
    container_name: pims-api.lh.local
    env_file: .env.local
    build:
      dockerfile: Dockerfile
      target: pims-image
      context: .
    volumes:
      - '.:/var/www/html:cached'
    depends_on:
      - db.lh.local
      - redis.lh.local

  pims-api-nginx.lh.local:
    container_name: pims-api-nginx.lh.local
    image: nginx:1.21
    restart: 'always'
    entrypoint:
      - '/usr/sbin/nginx'
      - '-c'
      - '/etc/nginx/nginx.conf'
      - '-g'
      - 'daemon off;'
    volumes:
      - '.:/var/www/html:cached'
      - './config/nginx/nginx.conf:/etc/nginx/nginx.conf'
      - './config/nginx/app.conf.local:/etc/nginx/conf.d/app.conf'
    ports:
      - 8100:80
    depends_on:
      - pims-api.lh.local
    links:
      - pims-api.lh.local

which I can reach on my localhost as http://pims-api.lh.local:8100/ however if a service from the other monolith tried to call that it will time out. I figure I need to configure a bridge between from the caller to the receivers external network.

However from the research I've done it looks like it can only join the pre-existing network.

https://docs.docker.com/compose/networking/

I have the following networks:

will$ docker network ls
NETWORK ID     NAME                     DRIVER    SCOPE
2e83faeabb3c   bridge                   bridge    local
159d8ffaace3   host                     host      local
abe217b17a92   pims-api_default         bridge    local
2f09770d776a   product_default          bridge    local

I wish to bridge pims-api_default and product_default

Kinda at a loss of what the correct approach is here. Sorry for the ramble but scratching my on this one.

Upvotes: 0

Views: 631

Answers (1)

BMitch
BMitch

Reputation: 265045

Between containers, they talk over the docker network, to the container port, not the published port on the host. So it would connect to port 80, the default for http, not 8100: http://pims-api.lh.local/. Publishing a port on the host creates a forward from the host network to the container, which isn't needed for containers to talk to each other.

Note that links are a legacy feature. I'd remove those, ensure the containers are on the same user created docker network (compose does this by default), and use the service name for connecting. The hostname is only useful if your application depends on it's own hostname being set (possible with a legacy app, but I'd try removing that once you get this working).

Upvotes: 1

Related Questions