The A file creates the network. The B file connects to it.
docker-compose -f docker-compose-A.yaml up -d
docker-compose -f docker-compose-B.yaml up -d
docker exec -it test_a ash
Then inside the container:
/ # apk add bind-tools
...
/ # echo test_a_service test_a a test_b_service test_b b blah | xargs -n 1 host
test_a_service has address 172.20.0.2
test_a has address 172.20.0.2
a has address 172.20.0.2
test_b_service has address 172.20.0.3
test_b has address 172.20.0.3
b has address 172.20.0.3
Host blah not found: 3(NXDOMAIN)