docker集装箱通过港口进行通信

我试图创build2个docker容器,并运行它们可以通过本地端口相互通信的方式。 我创build了2个Python文件作为发件人和接收者。 当我没有docker运行他们沟通很好。 但是docker工他们没有正常运行。

寄件人

Python脚本

#!/usr/bin/python # -*- encoding: utf-8 -*- import socket import time import sys print sys.argv[1] print sys.argv[2] for i in range(1,10): time.sleep(2) data = "My parameters that I want to share with the server on ith iteration %d" % (i) print "sedning data: %d" % (i) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((sys.argv[1], int(sys.argv[2]))) sock.sendall(data) sock.close() 

Dockerfile

 FROM ubuntu RUN \ apt-get update && \ apt-get install -y python python-dev python-pip python-virtualenv && \ rm -rf /var/lib/apt/lists/* ADD script.py /root/script.py CMD python -u /root/script.py $SEND_HOST $SEND_PORT 

接收器

Python脚本

 #!/usr/bin/python # -*- encoding: utf-8 -*- import socket import sys print sys.argv[1] print sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((sys.argv[1], int(sys.argv[2]))) s.listen(3) while True: conn, addr = s.accept() data = conn.recv(1024) conn.close() print "received data from sender: %s" % (data) 

Dockerfile

 FROM ubuntu RUN \ apt-get update && \ apt-get install -y python python-dev python-pip python-virtualenv && \ rm -rf /var/lib/apt/lists/* ADD script.py /root/script.py CMD python -u /root/script.py $LISTEN_HOST $LISTEN_PORT 

运行Receiver的命令

 docker run --name="testListen" -p 5555:5555 --env LISTEN_HOST="localhost" --env LISTEN_PORT="5555" docker.io/ayonnayihan/sample-sendr-rcv-test:receiver0.1 

运行发件人的命令

 docker run --name="testTalk" --env SEND_HOST="localhost" --env SEND_PORT="5555" docker.io/ayonnayihan/sample-sendr-rcv-test:sender0.1 

在运行容器之前,我确保两个图像都被构build。 任何人都可以告诉为什么它不正常运行?

这里是简单的python运行命令,没有docker工作正常:

在接收器python script.py localhost 5555

在发件人python script.py localhost 5555

我想你有3个select来实现这个工作:

  1. 创build一个dockernetworking来连接主机:

     docker network create --driver bridge sample-sendr-rcv-test docker run --name="testListen" --env LISTEN_HOST="0.0.0.0" --env LISTEN_PORT="5555" --network=sample-sendr-rcv-test -d docker.io/ayonnayihan/sample-sendr-rcv-test:receiver0.1 docker run --name="testTalk" --env SEND_HOST="testListen" --env SEND_PORT="5555" --network=sample-sendr-rcv-test -d docker.io/ayonnayihan/sample-sendr-rcv-test:sender0.1 
  2. docker-compose.yml -compose和docker-compose.yml一样:

     version: '2' services: sender: image: docker.io/ayonnayihan/sample-sendr-rcv-test:sender0.1 # build: sender environment: SEND_HOST: receiver SEND_PORT: 5555 receiver: image: docker.io/ayonnayihan/sample-sendr-rcv-test:receiver0.1 # build: receiver environment: LISTEN_HOST: '0.0.0.0' LISTEN_PORT: 5555 
  3. 使用主机联网:

     docker run --name="testListen" --env LISTEN_HOST="127.0.0.1" --env LISTEN_PORT="5555" --net=host -d docker.io/ayonnayihan/sample-sendr-rcv-test:receiver0.1 docker run --name="testTalk" --env SEND_HOST="localhost" --env SEND_PORT="5555" --net=host -d docker.io/ayonnayihan/sample-sendr-rcv-test:sender0.1 

第三种select是最接近你目前正在做的,但我不会推荐它,原因如下解释。 其他任何选项都可以,但如果你刚刚开始使用docker,可能不值得学习docker-compose。

你遇到问题的原因是每个容器都有自己的'localhost'的想法,因为它们位于不同的networking名称空间中 。 这意味着“testTalk”容器上的“localhost”不会parsing到侦听器正在运行的主机。 当你使用--net=host (上面的选项3)时,你将删除容器的单独命名空间,从而消除了使用--net=host一些安全性好处。