Docker Networking
Docker networks allow containers to communicate with each
other.
Example :
Frontend Container
│
▼
Backend Container
│
▼
Database Container
Without networking:
❌ Containers cannot
easily communicate.
With networking:
✅
Containers communicate securely.
In docker networks there are 6 types of networks -
1) bridge
2) host
3) overlay
4) ipvlan
5) macvlan
6)
none
so steps by steps we will learn each and every docker networks.
1. Bridge - Its a default network as a bridge and if
we will make container and don't make any other networks so its use by default
network as a bridge from our host machine.
So let's see how to
verify we have connected through bridge while making container. if you
are followed our docker's blog continuously then you know we have
already images , container that we are
already uploaded.
So, here we will run our docker desktop from our system (in windows search bar search docker desktop) are using our first command
- docker run -it --name server1 ubuntu bash
Here:
- server1 = Container Name
- ubuntu = Image Name
- bash = Command to run
Here a ubuntu server is running even you can verify container name
via
- docker ps
Lets verify this docker container is connected via internet.
So as you can see the old one images and in that container we are facing some
problems to apt-get or apt install iputils-ping -y because we
are unable to use ping command.
So we will create new one file, docker images and install manually so
follow the steps and do with scratch.
Step 1: Create a folder
networking-dev/ ├── Dockerfile
Step 2: Create Dockerfile
So go to your desire directory in your local machine and then create a folder
mkdir networking-dev
cd networking-dev
and inside the folder open vs code editor so type a file name without any extension.
code Dockerfile
add this command and save it simple.
FROM ubuntu:24.04
RUN apt-get update && \
apt-get install -y \
iputils-ping \
net-tools \
iproute2 \
dnsutils
CMD ["bash"]
This installs:
ping
ifconfig
ip
nslookup
other networking
tools
Step 3: Build your image
Go inside the folder containing the Dockerfile:
- docker build -t atul/networking-dev .
Notice the . at the end.
Docker will:
- Read the Dockerfile
- Download Ubuntu 24.04
- Install ping, net-tools, iproute2, dnsutils
- Create image atul/networking-dev
Verify:
docker images
You should see:
REPOSITORY TAG
atul/networking-dev
latest
Run container
Now lets move into our real purpose of this blog docker networking
via bridge
So, here we will run our docker desktop from our system (in windows search bar search docker desktop) are using our first command
- docker run -it --name server1 atul/networking-dev
Previously we have server1 in container docker desktop so delete it.
Inside container test:
ping google.com
ifconfig
ip
addr
nslookup google.com
All should work.
- docker ps
C:\Users\user\Desktop\app>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 98acba8d0340 atul/networking-dev "bash" 4 minutes ago Up 4 minutes server1 C:\Users\user\Desktop\app>
Verify which network a container is using
Run:
- docker inspect server1
Large output will appear.
To see only network information:
- docker inspect server1 --format='{{json .NetworkSettings.Networks}}'
Output:
{"bridge":{"IPAddress":"172.17.0.1"}}
This means:
Container: server1
Network: bridge
IP: 172.17.0.1
Practical Demo
Create two containers:
- docker run -dit --name server1 atul/networking-dev
- docker run -dit --name server2 atul/networking-dev
Check:
docker ps
After creating server in another terminal lets check IP address even try to ping each other
hostname -I
ping 172.17.0.2
We are able because both are connected with docker network by default bridge.
Enter server1:
docker exec -it server1 bash
Try:
ping
server2
On the default bridge network, name resolution may not work
as expected.
root@98acba8d0340:/# ping server2 ping: server2: Name or service not known root@98acba8d0340:/#
we are not able to ping because of they are only default network as an IP address.
A better practice is creating your own network: so for this open new
terminal and run this command
- docker network create mynetwork
open another terminal and type this command with server3 and --network mynetwork
hostname -I
you can see the IP there is - 172.18.0.2 but this is in another class - 18 and previous one are in 17 class
Now we want to connect server 1 with my network mynetwork
and you can see in server1 having two different class IP
now we are able to run using name
These are commands that we have used
docker network ls
docker inspect server1
docker network create
mynetwork
docker run --network mynetwork ...
Conclusion
Understanding Dockerfiles, custom image creation, and environment
variables is essential for anyone learning Docker, DevOps, Cloud Computing, or
System Administration.
By mastering these concepts, you'll be able
to create reusable Docker images, automate deployments, and configure
applications efficiently. These skills are widely used in real-world
production environments and are frequently asked about in DevOps and System
Engineer interviews.













0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.