IP address - Docker

Hi!
I have a problem:
I have a client side that transfers a file to a server side and checks that everything is fine and it works well locally on the computer (before I uploaded it to Docker)
(The code is python)
server:

import os
from socket import *
import select
import hashlib
import json
from datetime import datetime


# Assigning server IP and server port
serverName = "127.0.0.1"
serverPort = 5000
# Setting timeout
timeout = 3
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind((serverName, serverPort))
# While loop for the receiving of file
while True:
    data, serverAddress = serverSocket.recvfrom(1024)
    if data:
        decoded_data = json.loads(data.decode())
        md5_returned = hashlib.md5((decoded_data['file']).encode()).hexdigest()

        if json.loads(data.decode())['hash'] == md5_returned:
            print("MD5 verified OK.")
        else:
            print("MD5 verification failed!.")

        path = f'.\\recived_files'
        os.makedirs(path, exist_ok=True)
        file_name = f'{decoded_data["name"]}_{datetime.now().strftime("%H_%M_%S")}'

        f = open(f"{path}\\{file_name}.txt", "w")
        f.write(decoded_data['file'])
        f.close()

        print(f"File has been Received, located in {path}")

client:

import json
import os.path
from socket import *
import time
import hashlib
import json
from pathlib import Path

# Assigning server IP and server port
serverName = "127.0.0.1"
serverPort = 5000

# Setting buffer length
buffer_length = 500
# Assigning the audio file a name
file_path = defoult_file = r'./file.txt'
clientSocket = socket(AF_INET, SOCK_DGRAM)

while file_path != "0":
    file_path = input("enter file location")
    try:
        if file_path == "":
            file_path = defoult_file
        file_path = Path(file_path)
    except Exception as e:
        raise Exception('illegal location')
    if not os.path.exists(file_path):
        raise Exception("file doesn't exists")
    f = open(file_path, "rb")
    # Reading the buffer length in data
    file_data = f.read(buffer_length)
    hash = hashlib.md5(file_data).hexdigest()
    data = {'name': file_path.name[:-4], 'file': file_data.decode(), "hash": hash}

    if clientSocket.sendto(json.dumps(data).encode(), (serverName, serverPort)):
        data = f.read(buffer_length)
        time.sleep(0.02)  # waiting for 0.02 seconds

    print("the file sent to server")
clientSocket.close()
f.close()
print("File has been Transferred")

I’ve now uploaded both to Docker and I have no Error but I see they do not communicate with each other,
There may be a problem with the IP address,
I do not know the topic
It will really help me if you can help me with that
Thank you!
Rachel

Hello Rachel,
I guess you have created two containers - one for the client and one for the server. If that is true you may continue reading :slight_smile:
But a container’s 127.0.0.1 is not the docker-host’s 127.0.0.1. So you have to bind to 0.0.0.0 within the server’s script.
Now use the server-container’s name as destination within the client-script.
Or you can bind the server-container’s port 5000 to a docker-host’s port and within the client-container connect to the docker-host’s ip-address/-name (172.17.0.1 on Linux, host.docker.internal on Windows).
Best regards
Matthias

Thank you!! I’ll try !