Search code examples
dockerdocker-composedocker-network

Can't find Docker Compose network entry


I am trying to communicate from one Docker container running on my Win10 laptop with another container also running locally.

I start up the target container, and I see the following network:

docker network ls
NETWORK ID          NAME                                                DRIVER              SCOPE
...
f85b7c89dc30        w3virtualservicew3id_w3-virtual-service-w3id        bridge              

I then start up my calling container with docker-compose up. I can then successfully connect my other container to the network via the command line:

docker network connect w3virtualservicew3id_w3-virtual-service-w3id w3vacationatibmservice_rest_1

However, I can't connect to that same network by adding it to the network section of my docker-compose.yml file for the calling container. I was under the impression that they both basically did the same thing:

networks:
  - w3_vacation-at-ibm_service
  - w3virtualservicew3id_w3-virtual-service-w3id

The error message tells me it can't find the network, which is not true, since I can connect via the command line, so I know it's really there and running:

ERROR: Service "rest" uses an undefined network "w3virtualservicew3id_w3-virtual-service-w3id"

Anyone have any idea what I'm missing?


Solution

  • The network you define under your service is expected to be defined inside the global networks section (same thing for volumes):

    version 'X.Y'
    
    services:
      calling_container:
        networks:
          - your_network
    
    networks:
      your_network:
        external: true
    

    Do you really have to use a separate compose yml for your calling container? If both of your container interacts with each other, you should add them both to one and the same compose yml. In this case, you don't have to specifiy any network, they will automatically be inside the same network.