How to link Docker services across hosts?

前端 未结 8 1506
南笙
南笙 2021-01-29 17:51

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 wou

8条回答
  •  攒了一身酷
    2021-01-29 18:22

    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.

提交回复
热议问题