1. Setup Docker Containers

Open a terminal or command prompt window and start a docker container (name your container n1):

docker run -it --name=n1 ubuntu

Unfortunately the base ubuntu image has only bare minimal packages installed, so we need a few more packages:

apt-get -y update
apt-get -y install iputils-ping
apt-get -y install iproute
apt-get -y install dnsutils

To find out the IP address of the current container (from within the container):

ip a

Repeat the above in another window and name the container n2. Take note of the IP address of n2 (mine is 172.18.0.3). In the window for n1, test the connectivity with n2 using:

ping 172.18.0.3

2. Setup Python

Install python3, package installer, a text editor, and sqlite3:

apt-get -y install python3
apt-get -y install python3-pip
apt-get -y install vim
apt-get -y install sqlite3

It is best practice NOT to work in the root account, so you should create a user account (I am calling mine ly) using the following command and follow the onscreen instructions:

adduser ly

You can login to your user account using the su command and go to the home directory:

su ly
cd
mkdir test0
cd test0

Typically we develop and test the client-server program on one machine first before testing on two machines (i.e., containers). Copy and paste the following server.py

import socket

def Main():
    host = "127.0.0.1"
    port = 5000

    mySocket = socket.socket()
    mySocket.bind((host,port))

    mySocket.listen(1)
    conn, addr = mySocket.accept()
    print ("Server: Connection from " + str(addr))
    data = conn.recv(1024).decode()
    if not data:
        return
    print ("Server: recv " + str(data))

    data = "Hello from Server"

    print ("Server: send " + str(data))
    conn.send(data.encode())

    conn.close()

if __name__ == '__main__':
    Main()

Copy and paste the following client.py

import socket
 
def Main():
        host = '127.0.0.1'
        port = 5000
         
        mySocket = socket.socket()
        mySocket.connect((host,port))
         
        message = "Hello from client program!"
         
        print ('Client: send ' + str(message))
        mySocket.send(message.encode())
        data = mySocket.recv(1024).decode()
                 
        print ('Client: recv ' + str(data))
                 
        mySocket.close()
 
if __name__ == '__main__':
    Main()

Run the server in the background using

python3 server.py &

Run the client using

python3 client.py

Next Steps

Docker Primer

Linux System Admin Primer