Home > Enterprise >  docker client and server containers connection refused
docker client and server containers connection refused

Time:11-02

I want to connect client image and server image. So I tried to use --link, docker-compose.yml, but I failed. However, when I tried to connect local client code and server container, it succeed. I think it may be a problem of Dockerfile, but I can't fix it.. these are my code:

---server

import socket

HOST = socket.gethostbyname('localhost')
PORT = 65456  

print('> echo-server is activated')
#print(HOST,PORT)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serverSocket:
    serverSocket.bind(('', PORT))
    serverSocket.listen()
    clientSocket, clientAddress = serverSocket.accept()
    with clientSocket:
        print('> client connected by IP address {0} with Port number {1}'.format(clientAddress[0], clientAddress[1]))
        while True:
            # [=start=]
            RecvData = clientSocket.recv(1024)
            print('> echoed:', RecvData.decode('utf-8'))
            clientSocket.sendall(RecvData)
            if RecvData.decode('utf-8') == 'quit':
                break
            # [==end==]
print('> echo-server is de-activated')

---client

import socket

HOST = socket.gethostbyname('localhost')
PORT = 65456

print('> echo-client is activated')
#print(HOST,PORT)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as clientSocket:
    #print(HOST,PORT)    
    clientSocket.connect((HOST, PORT))
    while True:
        sendMsg = input("> ")
        clientSocket.sendall(bytes(sendMsg, 'utf-8'))
        recvData = clientSocket.recv(1024)
        print('> received:', recvData.decode('utf-8'))
        if sendMsg == "quit":
            break

print('> echo-client is de-activated')

---server Dockerfile

FROM python:latest
COPY . /me
RUN apt-get update

RUN mkdir -p /me
CMD ["python", "/me/server.py"]

EXPOSE 65456

---client Dockerfile

FROM python:latest
COPY . /you
RUN apt-get update

RUN mkdir -p /you
CMD ["python", "/you/client.py"]

EXPOSE 65456

This is echo program.

CodePudding user response:

Your code works for me.

You don't include the commands that you tried. These would be helpful additions to your question.

Build both containers. I using different Dockerfile names to disambiguate between the two.

Q="74282751"

docker build \
--tag=${Q}:server \
--file=./Dockerfile.server \
${PWD}

docker build \
--tag=${Q}:client \
--file=./Dockerfile.client \
${PWD}

Then in one shell, run the server container publishing the container's port 65456 on the host's port (65456):

docker run \
--interactive --tty --rm \
--publish=65456:65456 \
${Q}:server

And, in another shell, run the client container, binding the container to the host's network. This is an easy way to provide access to the host's 65456 port which now maps to the server container port 65456:

docker run \
--interactive --tty --rm \
--net=host \
${Q}:client
Feedback
  1. When you FROM python, you're actually implicitly referencing Docker's container registry. Always be explicit and FROM docker.io/python
  2. Try to never use :latest. In this case, you'd want to consider using the actual latest version of the Python container (i.e. 3.9.15).
  3. Conventionally WORKDIR is used to define a working directory in a container. This creates and changes to the folder and save RUN mkdir and repeatedly referencing the destination folder.
  4. Try to be explicit about what is being COPY'd. For the server, it's only server.py rather than . the entire directory.
  5. EXPOSE is for documentation only. It has no effect. It only applies to the server since the client doesn't expose any ports.
  6. Rather than hard-code constants (HOST,PORT) in your code, it's good practice to access these as environment variables (i.e. HOST = os.getenv("HOST")). This makes your code more flexible. Generally (!) for code that's destined to be containerized, you'll be able to default HOST to localhost (or 127.0.0.1 sometimes 0.0.0.0).
FROM docker.io/python:3.9.15

RUN apt-get update

WORKDIR /me

COPY server.py .

EXPOSE 65456

CMD ["python", "/me/server.py"]
  • Related