多主机Docker在Swarm上组成应用程序

我有一个简单的撰写文件,我有三个服务A,B和C.A和B都依赖于C(即有links到C)。 这里是一个来自docker-compose.yml的简化摘录:

 kafka: image: spotify/kafka environment: ADVERTISED_PORT: 9092 ports: - "2181:2181" - "9092:9092" ServiceA: image: elsoufy/myimage command: ./mycommand -role producer -queue kafka:9092 ports: - "8080" ServiceB: image: elsoufy/myimage command: ./mycommand -role consumer -queue kafka:9092 

我在AWS上设置了一个Docker swarm,并通过consul key-store启用覆盖networking。 我一直在挣扎一段时间才能正常工作(我不得不手动升级机器的内核到linux 3.16 )。

我使用的是Docker 1.9

 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 up -d成功启动docker-compose up -d ,但是ServiceAServiceB在尝试连接到kafka之后崩溃,因为他们找不到它。 当我尝试使用显式links并为这个应用程序启用覆盖, 我得到一个错误 :

 docker-compose --x-networking --x-network-driver=overlay up 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. 

它看起来像我不能使用links当我想启用多主机! 但是,那么我怎么能运行一个组合应用程序,我已经有了容器之间的依赖关系,并在docker群上扩展了容器?

你不需要明确的链接了。 networking应该链接在同一个用户定义的networking上的所有容器。 撰写基于项目名称为您创build一个默认networking。

要表示容器或服务之间的依赖关系,可以使用depends_on docker-compose.yml文件中的configuration选项depends_on ,但使用version 2文件格式。

所以你的docker-compose.yml文件应该看起来像这样:

 version: '2' services: kafka: image: spotify/kafka environment: ADVERTISED_PORT: 9092 ports: - "2181:2181" - "9092:9092" ServiceA: image: elsoufy/myimage command: ./mycommand -role producer -queue kafka:9092 ports: - "8080" depends_on: - kafka ServiceB: image: elsoufy/myimage command: ./mycommand -role consumer -queue kafka:9092 depends_on: - kafka networks: ... 

docker-compose up将以依赖性顺序启动服务。 在上面的例子中, kafka将在ServiceAServiceB之前启动。

注意:如果您使用的是version 1文件格式以及links ,您的应用程序将可以正常工作,但Swarm会将所有容器安排在一台主机上,因为容器之间的links不能在具有旧networking系统的主机上运行。