处理ipc的方法有很多种。这只是通过在目标容器(java 服务器)中公开端口和来自源容器(python 客户端)的链接的一种方法。
我为你创建了一个例子。在这种情况下,我是从 Python 连接到 Java。
我的文件夹结构如下:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ tree .
.
├── docker-compose.yml
├── javacl
│ ├── Dockerfile
│ ├── Main.class
│ ├── Main.java
│ └── OneConnection.class
└── pythoncl
├── client.py
└── Dockerfile
2 directories, 7 files
anovil@ubuntu-anovil:~/tmp/docker-ipc$
我的服务器 (source) 监听并输出它在端口 10001 上获得的任何信息:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ cat javacl/Main.java
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) throws Exception {
final int myPort = 10001;
ServerSocket ssock = new ServerSocket(myPort);
System.out.println("Listening on port " + myPort );
while (true) {
Socket sock = ssock.accept();
System.out.println("Someone has made socket connection");
OneConnection client = new OneConnection(sock);
String s = client.getRequest();
}
}
}
class OneConnection {
Socket sock;
BufferedReader in = null;
DataOutputStream out = null;
OneConnection(Socket sock) throws Exception {
this.sock = sock;
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new DataOutputStream(sock.getOutputStream());
}
String getRequest() throws Exception {
String s = null;
while ((s = in.readLine()) != null) {
System.out.println("got: " + s);
}
return s;
}
}
anovil@ubuntu-anovil:~/tmp/docker-ipc$
我的客户端连接到主机:java-server on port:10001 并发送“hello stackoverflow”:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ cat pythoncl/client.py
#!/usr/bin/python
#client example
import socket
import time
print "Waiting for socket"
time.sleep(3)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('java-server', 10001))
client_socket.send("hello")
client_socket.send(" stackoverflow")
client_socket.close()
while 1:
pass # do nothing
anovil@ubuntu-anovil:~/tmp/docker-ipc$
这些容器中的每一个只是分别调用服务器和客户端,除了服务器为其他容器公开 10001 端口并且这些 docker 文件如下所示:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ cat javacl/Dockerfile
FROM java:7
COPY Main.class OneConnection.class /
EXPOSE "10001"
CMD ["java","Main"]
anovil@ubuntu-anovil:~/tmp/docker-ipc$ cat pythoncl/Dockerfile
FROM python:2.7
COPY client.py /client.py
CMD python client.py
anovil@ubuntu-anovil:~/tmp/docker-ipc$
然后在 compose 文件中,javacl 容器被赋予一个主机名来标识自己,pythoncl 链接到 javacl,如下所示:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ cat docker-compose.yml
javacl:
build: ./javacl
hostname: java-server
pythoncl:
build: ./pythoncl
links:
- "javacl"
anovil@ubuntu-anovil:~/tmp/docker-ipc$
现在当你运行它时,
anovil@ubuntu-anovil:~/tmp/docker-ipc$ docker-compose up
Starting dockeripc_javacl_1
Starting dockeripc_pythoncl_1
Attaching to dockeripc_javacl_1, dockeripc_pythoncl_1
javacl_1 | Listening on port 10001
javacl_1 | Someone has made socket connection
javacl_1 | got: hello stackoverflow
...
故意让服务器和客户端连续运行,以便我们可以像这样分别检查它们:
anovil@ubuntu-anovil:~/tmp/docker-ipc$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c96961f03d57 dockeripc_pythoncl "/bin/sh -c 'python c" 13 minutes ago Up 19 seconds dockeripc_pythoncl_1
9d0163aa34f5 dockeripc_javacl "java Main" 13 minutes ago Up 19 seconds 10001/tcp dockeripc_javacl_1
anovil@ubuntu-anovil:~/tmp/docker-ipc$
可以通过attach 或exec 登录这些容器,看看会发生什么