如何通过“主机名”在Docker容器之间进行通信 [英] How to communicate between Docker containers via "hostname"

查看:507
本文介绍了如何通过“主机名”在Docker容器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将我的单管服务器拆分成许多小的码头集装箱,但尚未找到集装箱间通信的良好解决方案。这是我的目标场景:





我知道如何将容器连接在一起,以及如何暴露端口,但是这些解决方案都没有令我满意。



是否有任何通过主机名(容器名称)在传统服务器网络之间进行通信的解决方案?

解决方案

编辑:在Docker 1.9之后, docker network 命令(见下文 https://stackoverflow.com/a/35184695/977939 )是实现此目的的推荐方法。






我的解决方案是在主机上设置一个dnsmasq以自动更新DNS记录:A记录具有容器的名称,并指向IP地址的容器自动(每10秒)。 自动更新脚本<​​/a>已粘贴到此处:

 #!/ bin / bash 

#默认为10秒间隔时间
INTERVAL = $ {INTERVAL:-10}

#dnsmasq config目录
DNSMASQ_CONFIG = $ {DNSMASQ_CONFIG: - 。}

此脚本中使用的#命令
DOCKER = $ {DOCKER: - docker}
SLEEP = $ {SLEEP:-sleep}
TAIL = $ {TAIL:-tail}

声明-A service_map

while true
do
changed = false
while read line
do
name = $ {line ## *}
ip = $($ {DOCKER} inspect - format'{{.NetworkSettings.IPAddress}}'$ name)
如果[-z $ {service_map [$ name]}] || [$ {service_map [$ name]}!= $ ip]#IP addr更改
然后
service_map [$ name] = $ ip
#写入文件
echo $ name有一个新的IP地址$ ip>& 2
echohost-record = $ name,$ ip> $ {DNSMASQ_CONFIG} / docker- $ name
changed = true
fi
done< <($ {DOCKER} ps | $ {TAIL} -n +2)

#发生IP地址更改,重新启动dnsmasq
如果[$ changed = true]
然后
systemctl restart dnsmasq
fi

$ {SLEEP} $ INTERVAL
done

确保您的dnsmasq服务在 docker0 上可用。然后,使用 - dns HOST_ADDRESS 启动您的容器以使用此迷你dns服务。



参考: http://docs.blowb.org/setup-host/dnsmasq.html


I plan to split my monolthic server up into many small docker containers but haven't found a good solution for "inter-container communication" yet. This is my target scenario:

I know how to link containers together and how to expose ports, but none of these solutions are satisfying to me.

Is there any solution to communicate via hostnames (container names) between the containers like in a traditional server network?

解决方案

Edit: After Docker 1.9, the docker network command (see below https://stackoverflow.com/a/35184695/977939) is the recommended way to achieve this.


My solution is to set up a dnsmasq on the host to have DNS record automatically updated: "A" records have the names of containers and point to the IP addresses of the containers automatically (every 10 sec). The automatic updating script is pasted here:

#!/bin/bash

# 10 seconds interval time by default
INTERVAL=${INTERVAL:-10}

# dnsmasq config directory
DNSMASQ_CONFIG=${DNSMASQ_CONFIG:-.}

# commands used in this script
DOCKER=${DOCKER:-docker}
SLEEP=${SLEEP:-sleep}
TAIL=${TAIL:-tail}

declare -A service_map

while true
do
    changed=false
    while read line
    do
        name=${line##* }
        ip=$(${DOCKER} inspect --format '{{.NetworkSettings.IPAddress}}' $name)
        if [ -z ${service_map[$name]} ] || [ ${service_map[$name]} != $ip ] # IP addr changed
        then
            service_map[$name]=$ip
            # write to file
            echo $name has a new IP Address $ip >&2
            echo "host-record=$name,$ip"  > "${DNSMASQ_CONFIG}/docker-$name"
            changed=true
        fi
    done < <(${DOCKER} ps | ${TAIL} -n +2)

    # a change of IP address occured, restart dnsmasq
    if [ $changed = true ]
    then
        systemctl restart dnsmasq
    fi

    ${SLEEP} $INTERVAL
done

Make sure your dnsmasq service is available on docker0. Then, start your container with --dns HOST_ADDRESS to use this mini dns service.

Reference: http://docs.blowb.org/setup-host/dnsmasq.html

这篇关于如何通过“主机名”在Docker容器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆