![同一本地网络中不同主机上的 Docker 容器之间的通信](https://linux22.com/image/1511605/%E5%90%8C%E4%B8%80%E6%9C%AC%E5%9C%B0%E7%BD%91%E7%BB%9C%E4%B8%AD%E4%B8%8D%E5%90%8C%E4%B8%BB%E6%9C%BA%E4%B8%8A%E7%9A%84%20Docker%20%E5%AE%B9%E5%99%A8%E4%B9%8B%E9%97%B4%E7%9A%84%E9%80%9A%E4%BF%A1.png)
我有两个 Docker 容器在两个不同的主机上运行:
- 计算机 A(PC,使用以太网),ip 192.168.0.11 [Docker 容器在 172.17.0.2 上运行],操作系统 Windows 7
- 计算机 B(笔记本电脑,使用 WLAN),ip 192.168.0.12 [Docker 容器在 172.17.0.2 上运行],操作系统 Linux Ubuntu
我想以某种方式连接它们,以便它们可以相互通信(例如通过 ssh)。
我尝试通过以下方式运行容器来“链接”它们:
docker run --name master_node --rm -it branislava/ubuntu1504java8:v1
docker run --rm -it --link master_node:master_node1 --name slave_node branislava/ubuntu1504java8:v1
正如评论中所建议的。这是可行的;容器可以通信 - 但只有当它们在同一台主机上运行时才可以。
对于在同一本地网络中不同机器上运行的容器,如何实现这一点?
答案1
以下是在同一本地网络中不同主机上建立同一镜像的 docker 容器之间的通信的 bash 脚本:
# Firstly, we have to make a swarm
# We have to discover ip address of the swarm leader (i.e. manager, master node)
ifconfig
# My master computer has ip 192.168.0.12 in the local network
sudo docker swarm init --advertise-addr 192.168.0.12
# Info about swarm
sudo docker info
# info about nodes (currently only one)
sudo docker node ls
# hostaname got it name automatically, by the host's name
# adding new nodes to the swarm
# ssh to the slave node (i.e. worker node) or type physically:
# this command was generated after
# sudo docker swarm init --advertise-addr 192.168.0.12
# on manager computer
docker swarm join --token SWMTKN-1-55b12pdctfnvr1wd4idsuzwx34vcjwv9589azdgi0srgr3626q-01zjw639dyoy1ccgpiwcouqlk 192.168.0.12:2377
# if one cannot remember or find this command
# should type again on the manager host
docker swarm join-token worker
# ssh to the manager or type in directly:
# (listing existing nodes in a swarm)
sudo docker node ls
# adding docker image as a process that will run in this containers
# ssh to the manager or type in directly:
# replicas will be the number of nodes
# manager is also a worker node
sudo docker service create --replicas 2 --name master_node image-name sleep infinity
# I have to enter inside of the containers and set up some things before
# running application, so I say `sleep infinity`
# Else, this is not necessary.
# what's up with the running process
sudo docker service inspect --pretty etdo0z8o8timbsdmn3qdv381i
# or
sudo docker service inspect master_node
# also, but only from manager
sudo docker service ps master_node
# see running containers (from worker or from manager)
sudo docker ps
# promote node from worker to manager
# `default` is the name of my worker node
sudo docker node promote default
# denote node from manager to worker
sudo docker node demote default
# entering container, if needed
# getting container id with `sudo docker ps`
sudo docker exec -it bb923e379cbd bash
# retrieving ip and port of the container
# I need this since my containers are communicating via ssh
sudo docker ps
sudo docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 5395cdd22c44
# removing processes from all nodes
sudo docker service rm master_node
# this command should say `no process`
sudo docker service inspect master_node
希望有人会发现这很有用。
答案2
向我们展示 docker 命令。
我认为你的命令缺少链接
这是一个例子
docker run --name=redmine -it --rm --link=postgresql-redmine:postgresql \
--volume=/srv/docker/redmine/redmine:/home/redmine/data \
sameersbn/redmine:3.3.1
答案3
我从来没有尝试过,但你可以完成其他电脑
docker run --name=Client -it --rm \
--env='MEMCACHE_HOST=192.168.1.12' --env='MEMCACHE_PORT=22' \
答案4
我知道这是旧帖子。如果在容器上设置了 ssh 并且打开了端口 22,则需要找出用于外部连接的映射端口。这不是解决方案,但您可以尝试
在笔记本电脑 B 上运行此命令
docker port master_node1 22
如果端口是开放的,它会给你一些端口号,否则会输出错误,例如如果上述命令的输出是
0.0.0.0:1234
从笔记本电脑 A ssh 到笔记本电脑 B 上的容器
ssh [email protected] -p 1234