User defined network in Docker with an Example
Hello, my name is Sai Ram Peesapati and in this post I am going to walk you through user defined network in docker.
I hope by now you know what is docker if not click here to know about it.
Docker by default comes with three networks, they are none, bridge and host. if you want to learn more about them click here. But docker also gives you privilege to create your own network and called them as user defined network.
What is user defined network?
A network which is created by the user, so that containers which are running in that network can talk to each other without using the legacy link. User defined networks will have automatic DNS resolution of container names to IP addresses, because of this containers can talk to each other.
If you already have docker in your computer you are good to go but if you don't have it, no problem click here and follow the installation steps for your operating system.
Now let's start running your first container in user defined network.
To make it easy I make use of a simple python program using sockets, in which server is waiting for a client to connect and after the connection is established, server and client will send a hello message to each other and then closes the connection.
I hope by now you know what is docker if not click here to know about it.
Docker by default comes with three networks, they are none, bridge and host. if you want to learn more about them click here. But docker also gives you privilege to create your own network and called them as user defined network.
What is user defined network?
A network which is created by the user, so that containers which are running in that network can talk to each other without using the legacy link. User defined networks will have automatic DNS resolution of container names to IP addresses, because of this containers can talk to each other.
If you already have docker in your computer you are good to go but if you don't have it, no problem click here and follow the installation steps for your operating system.
Now let's start running your first container in user defined network.
To make it easy I make use of a simple python program using sockets, in which server is waiting for a client to connect and after the connection is established, server and client will send a hello message to each other and then closes the connection.
I am using Windows 10 Home, and here are the steps that I followed while creating the
Dockerfile, docker images and docker containers for server and client.
Step 1: Open Docker
Starting
running two Docker Quickstart Terminal one for server and one for client. You will see a two window's looks like the image showed below.
For some reasons I removed some information about my PC from the images.
For some reasons I removed some information about my PC from the images.
Step 2: Creating python code and Dockerfile for server and client
Terminal 1:
Create a folder with name server using commandmkdir server
Now go to server
folder by typing
cd server
Create a Dockerfile using the command
touch Dockerfile.
To check whether the Dockerfile is created or not just type ls and hit enter, then you can see the Dockerfile.
cd server
Create a Dockerfile using the command
touch Dockerfile.
To check whether the Dockerfile is created or not just type ls and hit enter, then you can see the Dockerfile.
Type vi Server.py to
create a python script to write our server code. Hit Insert button to start
typing and once you are done hit ESC and type :wq and hit enter to go back to docker terminal.
Type vi Dockerfile
to create Dockerfile for building the image. Hit Insert button to start typing
and once you are done hit ESC and type :wq and hit enter to go back to docker terminal.
Terminal 2:
Create a folder with name client using commandmkdir client
Now go to client folder by typing
cd client
Create a Dockerfile by using the command
touch Dockerfile
cd client
Create a Dockerfile by using the command
touch Dockerfile
Type vi Client.py to
create a python script to write our client code. Hit Insert button to start
typing and once you are done hit ESC and type :wq and hit enter to go back to docker terminal.
Type vi Dockerfile
to create Dockerfile for building the image. Hit Insert button to start typing
and once you are done hit ESC and type :wq and hit enter to got back to docker terminal.
The image below shows commands typed the order that I mentioned before in both terminals
The image below shows commands typed the order that I mentioned before in both terminals
The python code for server and client are given below.
Server Code:
from socket import *
s=socket(AF_INET,SOCK_STREAM)
s.bind(('',8000)) #Binding the socket to 8000 port
s.listen(5)
c,addr=s.accept()
c.send('hello from server'.encode())
d=c.recv(1024)
print(d)
c.close()
Client Code:
from socket import *
s=socket(AF_INET,SOCK_STREAM)
s.connect(('server',8000)) # why bind to server is explained below
d=s.recv(1024)
print(d)
s.send('hello from client'.encode())
The Dockerfile for server and client are given below
Server Dockerfile:
#starting from base
image alpine
FROM alpine
#installing updates
and python
RUN apk update
RUN apk add python
#copying my server
code
ADD Server.py
/Server.py
#exposing the port
EXPOSE 8000
#running the server
CMD
["python", "./Server.py"]
Client Dockerfile:
#starting from base
image alpine
FROM alpine
#installing updates
and python
RUN apk update
RUN apk add python
#copying my client
code
ADD Client.py
/Client.py
#running the client
CMD
["python", "./Client.py"]
Step 3: Creating a user defined network:
Create a user
defined network with name "new" by typing the command
docker network create new
because we want to run server and client in a user defined network.
docker network create new
because we want to run server and client in a user defined network.
Step 4: Building Images
Server Image:
Let's start building the image for server while current directory is in the server folder using the command "docker build -t "your docker id"/server .".
docker build -t sairam3k5/server . // here sairam3k5 is my docker id, -t is used to specify the tag for the image and don't forget to add period(.) at last.
Image shows how docker is building an image for our server Dockerfile.
docker build -t sairam3k5/server . // here sairam3k5 is my docker id, -t is used to specify the tag for the image and don't forget to add period(.) at last.
Image shows how docker is building an image for our server Dockerfile.
Client Image:
Let's start building the image for client while current directory is in the client folder using the command "docker build -t "your docker id"/client .".
docker build -t sairam3k5/client . //here sairam3k5 is my docker id, -t is used to specify the tag for the image and don't forget to add period(.) at last.
Image shows how docker is building an image for our client Dockerfile.
Image shows how docker is building an image for our client Dockerfile.
Step 5: Running the Containers
Server Container:
The previous command will build the image for the Dockerfile in the server folder and assigns an Image ID to itdocker run --net new --name server sairam3k5/server
and this will start server container by name server.
Why bind to server in the Client.py?
Containers running in user defined network can communicate with each other using the name of the container. and I gave the name "server" for the server container so that client container can resolve the IP address of server container by using the name "server".
Client Container:
The previous command
will build the image for the Dockerfile in the client folder and assigns an
Image ID to it
Use the image to run
the container in the new network by using the command
docker run --net new --name client sairam3k5/client
and this will start server container by name client.
docker run --net new --name client sairam3k5/client
and this will start server container by name client.
Output:
When the client
container is started running we can see the messages shared between client and
servers.
Note: The images sairam3k5/server and sairam3k5/client are in my docker repository and you can run the following commands from two separate terminals to pull both the images and run the containers.
docker run --net new --name server sairam3k5/server
docker run --net new --name client sairam3k5/client
Note: The images sairam3k5/server and sairam3k5/client are in my docker repository and you can run the following commands from two separate terminals to pull both the images and run the containers.
docker run --net new --name server sairam3k5/server
docker run --net new --name client sairam3k5/client
This comment has been removed by the author.
ReplyDelete