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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With