Reputation: 541
I am trying to communicate between two containers, but unable to do the same. I add a service named 'server' in docker compose dependent on another service named 'endpoint'. I am need to pass the ip and port of 'endpoint' to 'server', and i am doing the same as below
command: -userver -e endpoint:51393
But now on i don't see 'endpoint' in the list of hosts using below command:
docker-compose exec server cat /etc/hosts
Here is output:
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.21.0.3 49f4808fa677
My Full docker-compose.yml file is as below:
version: '2'
services:
endpoint:
build:
context: .
dockerfile: Dockerfile
image: myimage_end:0.1
command: -uendpoint
ports:
- 51393:51393
networks:
- internal
server:
build:
context: .
dockerfile: Dockerfile
image: myimage_server:0.1
command: -userver -e endpoint:51393
ports:
- 51392:51392
depends_on:
- endpoint
networks:
- internal
volumes:
- /local_path/dataset:/dataset
networks:
internal:
driver: bridge
Upvotes: 0
Views: 577
Reputation: 12240
When you use a current version of docker compose, reaching one container from another is no longer done by manipulating a container's hosts file, because this is a pretty static setup.
Nowadays docker-compose uses some kind of dns service that allows all containers in the same network to see each other under their service names. This allows dependent containers to be stopped without causing a depending container to also stop/restart. For this to work, you also don't need to specify depends_on
. This is only required to trigger a restart of the depending service when a dependency is restarted.
For more details, please have a look at the docker-compose docs about networking.
Some commands for debugging your situation:
docker-compose exec server nslookup endpoint
docker inspect <container_name_of_service>
Upvotes: 2