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