0

A docker-container is not able to connect to another docker-container in the same network.

Hello,

I'm really new in docker so I try to be more explizit as I can:

I wrote two python files to test how xmlrpc works (original code from https://docs.python.org/3/library/xmlrpc.client.html#module-xmlrpc.client ).

One is to implement the server :

from xmlrpc.server import SimpleXMLRPCServer

import socket

def is_even(n):
    return n % 2 == 0

file1 = open("myFile.txt", "w")

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(is_even, "is_even")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
ipAddr = socket.gethostbyname(hostname)

print("My computername is: " + hostname)
file1.write("My computername is: " + hostname + "\n")
print("My IP is:" + ipAddr)
file1.write("My IP is:" + ipAddr + "\n")

result = sock.connect_ex(('127.0.0.1', 8000))
if result == 0:
    print("Port is open")
    file1.write("Port is open\n")
else:
    print("Port is not open")
    file1.write("Port is not open\n")
sock.close()
file1.close()
server.serve_forever()


The other is for the client:

import xmlrpc.client

import socket

# server="http://localhost:8000/" when running on host
server="http://172.18.0.2:8000/"

# For debugging purposes
file1 = open("myFile.txt", "w")
print(server)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
ipAddr = socket.gethostbyname(hostname)
print("My computername is: " + hostname)
print("My IP is:" + ipAddr)
file1.write("My computername is: " + hostname + "\n")
file1.write("My IP is:" + ipAddr + "\n")
file1.close()

try:
    with xmlrpc.client.ServerProxy(server) as proxy:
        print("3 is even: %s" % str(proxy.is_even(3)))
        print("2 is even %s" % str(proxy.is_even(2)))
except:
    # To avoid the docker to end/exit
    i = 0
    while i < 1:
        print("Stay forever here")

Foreach file a Dockerfile was written:

FROM python:3

WORKDIR /usr/src/app

COPY src/server.py .

CMD ["python", "./server.py"]

and

FROM python:3

WORKDIR /usr/src/app

COPY src/server.py .

CMD ["python", "./server.py"]

The path of the projects looks like this:

~/test_networking/xmlrpcserver/Dockerfile
~/test_networking/xmlrpcserver/src/server.py
~/test_networking/xmlrpcclient/Dockerfile
~/test_networking/xmlrpcclient/src/client.py

Then I successfully create a network called myNetwork for testing:

$ docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
40a476487398   bridge      bridge    local
fede3c767292   host        host      local
114185ba5fcc   myNetwork   bridge    local
1a4b0391adff   none        null      local

Fromyour text ~/test_networking/xmlrpcserver I ran docker build . --tag python_server:v1.0

and from ~/test_networking/xmlrpcclient I ran docker build . --tag python_client:v1.0

I started the server with the command docker run --net myNetwork --name python_server python_server:v1.0 and this works very, well.

However when I start the client with the command docker run --net myNetwork --name python_client python_client:v1.0 I got the exception that the socket can't connect.

The try-catch was inserted to have the possibility to debug a little the containers ( as good as I can/understood ):

docker inspect myNetworkshow:

[
    {
        "Name": "myNetwork",
        "Id": "114185ba5fcc1b681977651b0a85689a3519a5a137aa87bf9d25ae6a2c4328a9",
        "Created": "2023-06-23T08:56:25.528818186+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "632b2f55ad8cd8e1ad253c9c6903c58b9f0fcd0c870d33583f59a7b7e31bc97c": {
                "Name": "python_server",
                "EndpointID": "a29692933e7357e79de5f67f8f53bfb513c52ccc41bc21cfc089219fd1d2f1de",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "86d8e34df94c8facc68a8a74969bd63a8a76a4f29728c72f5e95314f48bc3b67": {
                "Name": "python_client",
                "EndpointID": "6fdb4ddc806df309d647cc2bcf6034cf9386dbb382dc78979f50687836f0c82c",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

With docker exec -it python_server /bin/bash I could see on myText.txt that it has really the right IP and the port is open.

I run the exec command for the client to and the debug file was fine, too. However I could not ping to the server because the ping command does not exists on the container.

I read about exposing ports but in my understanding it does not solve the problem because it is to "forward" a port to the host - I tried it anyway but without success.

I do not know how to proceed now. Maybe I'm missing some options!?

I ran the server and the client directly on the host (Linux/Ubuntu) and there both works fine.

Any help is appreciated.

Regards,

Layer8

Layer8
  • 1
  • Your server is listening on `localhost`, but since each container is its own `localhost`, that makes it unreachable from outside its own container. Change the listener to `("0.0.0.0", 8000)`. Also see [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip). – David Maze Jun 23 '23 at 10:56
  • Thank you @DavidMaze Now that it works it seems so obvious. – Layer8 Jun 23 '23 at 12:49

0 Answers0