Compose: Circular import between container1 and container2

Created on 21 Nov 2014  路  16Comments  路  Source: docker/compose

I am hitting an issue where I need 2 containers to be able to communicate with each other, ie one container needs to be able to ping the other and vice versa but when I add each container in to the links section of the fig.yml file I get a Circular import between container1 and container2 error when I run fig up.

Is there a workaround for this? Isn't the link section just adding an entry in each containers host file? If so, shouldn't that be allowed for containers to be able to communicate with each other?

Here is the bit causing problems. Obviously I have changed some of the names around.

container1:
  image: user/container1
  ports:
    - "8000:8000"
  links:
    - "container2:container2"

container2:
  image: user/container2
  ports:
    - "8080:8080"
  links:
    - "container1:container1"

Is there something I am missing here?

Most helpful comment

this issue is evil ...

All 16 comments

Today, this doesn't work. The plan for linksv2 is to allow essentially this.
That said, in github.com/cpuguy83/docker-grand-ambassador you can create an ambassador container and each container can link to that..

so...

docker run -d --name amb -v /var/run/docker.sock:/var/run/docker.sock cpuguy83/docker-grand-ambassador -name container1 -name container2
docker run --name container1 -p 8000:8000 --link amb:container2 user/container1
docker run --name container2 -p 8080:8080 --link amb:container1 user/container2

Is this possible to do with fig? If so, would the following syntax be correct? I'm not completely sure how to specify that a container should run in detached mode within the fig yml.

ambassador:
    image: cpuguy83/docker-grand-ambassador
    volumes:
         - "/var/run/docker.sock:/docker.sock"
    name:
         - "container1"
         - "container2"

container1:
    image: user/container1
    ports:
        - "8000:8000"
    links:
        - "ambassador:container1"

container2:
    image: user/container2
    ports:
        - "8080:8080"
    links:
        - "ambassador:container2"

Detached doesn't matter.

The "name" param is an argument to the command in the container.

@cpuguy83 would you have a reference config for docker-grand-ambassador with fig?

@willdurand here's what worked for me...

ambassador:
    image: cpuguy83/docker-grand-ambassador
        volumes:
            - "/var/run/docker.sock:/var/run/docker.sock"
    command: "-name figname_container1_1 -name figname_container2_1"

container1:
    image: user/container1
    ports:
        - "8000:8000"
    links:
        - "ambassador:container2"

container2:
    image: user/container2
    ports:
        - "8080:8080"
    links:
        - "ambassador:container1"

@alt234 came up with the same config, thanks!

another way should be to use a "service discovery" and automatic dns registration like skydock+skydns. This way no need to use links. Well except if you use multihosts (because skydock/skydns is not ready for that).

You can create bidirectional links using a custom tiny DNS server. So instead of this

serviceb:
    image: username/serviceb

servicea:
    image: username/servicea
    links:
        - serviceb

Try this

dnsdock:
    image: tonistiigi/dnsdock
    volumes:
        - /var/run/docker.sock:/run/docker.sock
    ports:
        - 172.17.42.1:53:53/udp
serviceb:
    image: username/serviceb
    dns: 172.17.42.1

servicea:
    image: username/servicea
    dns: 172.17.42.1

You can access the services with: <container-name>.<image-name>.docker hostname. You can check <container-name> and <image-name> after docker-compose up in dnsdock logs. More details here

This is being addressed in #1676

Sorry, late to the party. @dnephin, I still get circular dependency error in 1.9.0. Having read #1676 and network docs I don't yet have a suitable solution with out using work around listed above. Use case is two containers that load balance and cluster to each other.

Simplified yml

gateway-member1:
  image: gateway.ex.base
  links:
    - gateway-member2:cluster.member.com
  hostname: gateway.example.com
  command: gateway.start --config conf/gateway-cluster-config.xml
gateway-member2:
  image: gateway.ex.base
  links:
    - gateway-member1:cluster.member.com
  hostname: gateway.example.com
  command: gateway.start --config conf/gateway-cluster-config.xml

my versions

Client:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   76d6bc9
 Built:        Tue Nov  3 19:20:09 UTC 2015
 OS/Arch:      darwin/amd64

Server:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   76d6bc9
 Built:        Tue Nov  3 19:20:09 UTC 2015
 OS/Arch:      linux/amd64

docker-compose version: 1.5.0
docker-py version: 1.5.0
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1j 15 Oct 2014

I'd prefer to use just compose and docker with only my containers (No ambassador pattern, no DNS containers, no network plugins).

Are you using the --x-networking flag to docker-compose ? Can you include the command you are running and the output ?

Thanks @dnephin, its working with the networking commands now! I was missing the flag. One question. Our application has a quirk for binding on clustering it assumes you know the interface name for the configuration (inherited by old hazelcast libraries). We are working on a patch on our end to configure it via domain names. In the mean time can we count on the interface names that the container sees as being deterministic?

docker-compose --x-networking -f jms.cluster.yml up
WARNING: 
Some services ("gateway-member1", "gateway-member2") define links, which are not compatible with Docker networking and will be ignored.
Future versions of Docker will not support links - you should remove them for forwards-compatibility.

I'm not sure about that. From what I've seen it's always eth0, but I'm not sure that's a guarantee. There might be something about it in the docker networking docs.

this issue is evil ...

Sad.

Hit this also.

Was this page helpful?
0 / 5 - 0 ratings