Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access container from another compose that connected to external network?

Here is compose file with config of container that I wish to connect to from external container (defined in another compose file):

version: '3.5'
services:
  service-to-connect-to:
    build: .
    networks:
      - my-external-network

networks:
  my-external-network:
    external: true

and another compose file that contains config for container from which I wish to connect to service-to-connect-to:

version: "3.5"

services:
  service-to-connect-from:
    build: .

I tried to connect to service-to-connect-to via this domains:

service-to-connect-to service-to-connect-to.my-external-network my_external_network.service-to-connect-to

but nothing of them worked.

Where I'm wrong?

Thanks

like image 971
Vassily Avatar asked Jul 25 '18 15:07

Vassily


1 Answers

First, you have to add both services to same network in order to connect them. So, the latter compose file should be something like

version: "3.5"

services:
  service-to-connect-from:
    build .
    networks:
      - my-external-network

networks:
  my-external-network:
    external: true

Now that both services are on the same network they can find each other using container's name. Container name is by default same as the service name BUT docker compose also prefixes it by project name which is by default the directory name where the compose file exists. You can see this if you first start the services by docker-compose up -d and then see how the containers get named by running docker ps. The container name could be for example project1_service-to-connect-to. With this name you can connect from another service.

If you like to, you can also set the container's name explicitly using container_name option for service. When used, compose doesn't prefix the container name anymore.

like image 134
ronkot Avatar answered Oct 03 '22 08:10

ronkot