How to link Docker services across hosts?

2020-05-10 23:58发布

Docker allows servers from multiple containers to connect to each other via links and service discovery. However, from what I can see this service discovery is host-local. I would like to implement a service that uses other services hosted on a different machine.

There have been several approaches to solving this problem in Docker, such as CoreOS's jumpers, host-local services that essentially proxy to the other machine, and a whole bunch of github projects for managing Docker deployments that appear to have attempted to support this use-case.

Given the pace of development it is hard to follow what current best practices are. Therefore my question is essentially:

  1. What (if any) is the current predominant method for linking across hosts in Docker, and
  2. Are there any plans for supporting this functionality directly in the Docker system?

标签: docker
8条回答
萌系小妹纸
2楼-- · 2020-05-11 00:26

Update

Docker 1.12 contains the so called swarm mode and also adds a service abstraction. They probably aren't mature enough for every use case, but I suggest you to keep them under observation. The swarm mode at least helps in a multi-host setup, which doesn't necessarily make linking easier. The Docker-internal DNS server (since 1.11) should help you to access container names, if they are well-known - meaning that the generated names in a Swarm context won't be so easy to address.


With the Docker 1.9 release you'll get built in multi host networking. They also provide an example script to easily provision a working cluster.

You'll need a K/V store (e.g. Consul) which allows to share state across the different Docker engines on every host. Every Docker engine need to be configured with that K/V store and you can then use Swarm to connect your hosts.

Then you create a new overlay network like this:

$ docker network create --driver overlay my-network

Containers can now be run with the network name as run parameter:

$ docker run -itd --net=my-network busybox

They can also be connected to a network when already running:

$ docker network connect my-network my-container

More details are available in the documentation.

查看更多
萌系小妹纸
3楼-- · 2020-05-11 00:38

Seems like docker swarm 1.14 allows you to:

  • assing hostname to container, using --hostname tag, but i haven't been able to make it work, containers are not able to ping each other by assigned hostnames.

  • assigning services to machine using --constraint 'node.hostname == <host>'

查看更多
登录 后发表回答