How to make a client and server communicate with each other in a user defined network in docker?

I have a basic client server program written in python which I have containerized using docker. In this program, I want the client to request to the server to display a html file on the client. In one container, I have the client and in another the server. I want these containers to communicate in a user defined docker network and with that have the client communicate with the server. I have tried all the basic stuff but still unable to do get a solution. I’ll be attaching the codes too. Can one someone please help me out with the appropriate solution ?

Here is the code for client.py along with the respective dockerfile

import urllib.request
fp = urllib.request.urlopen("http://localhost:9000/")

encodedContent = fp.read()
decodedContent = encodedContent.decode("utf8")

print(decodedContent)

# Close the server connection.
fp.close()

Now, the dockerfile

 FROM python:alpine3.10
 COPY client.py /client/

 WORKDIR /client/

 CMD python ./client.py

Here is the code for server.py along with the respective dockerfile

import http.server
import socketserver

handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", 9000), handler) as httpd:
# This instruction will keep the server running, waiting for requests from the client.
      httpd.serve_forever()

Now, the dockerfile

FROM python:alpine3.10
WORKDIR /server/
EXPOSE 9000
COPY server.py /server/
COPY index.html /server/
CMD python ./server.py

Just start them from a compose file, then you get this network automatically.

I did that and still not working.

Please show your compose file, otherwise it’s very difficult to help you.

I don’t want to do it using docker compose initially as I am just an amateur. I want to accomplish it using docker run and then docker compose. So, if you don’t mind, can you help me with that ? By the way, whenever I am running these containers in a user-defined network, I am getting two errors: OSError: [Errno 99] Address not available and urllib.error.URLError: <urlopen error [Errno 99] Address not available>.

docker compose makes everything 10 times easier, especially because it sets up all the network stuff and name resolving for you.

Thanks for your help but I got the solution. In the client.py, I just had to replace localhost with the name of the server container.
I have another program which I am facing problem towards. Here is the link: https://stackoverflow.com/questions/65344980/download-file-from-server-container-to-client-container-in-docker

Can you please help me out with the solution to this ?