Networking in docker
- Docker networking model follows a specification called as CNM (Container Networking Model)
- Docker networking supports
- Lets Discuss Single Host
- Docker supports Network drivers. The network drivers are
- Null
- HOST
- BRIDGE
- OVERLAY (multihost)
- Creating a bridge network
docker network create -d bridge --subnet 10.255.0.0/24 my_bridge
- running the container on the created network
docker run -d -P --net my_bridge nginx
-
All the containers attached a same network can communicate with each other using ip addresses.
-
Lets create two containers in default bridge
docker run -d --name b1 alpine sleep 1d
docker run -d --name b2 alpine sleep 1d
- Lets get ipaddress of b1 and b2
docker network inspect bridge
- we got ips as 172.17.0.2 and 172.17.0.3
- lets ping b2 from b1 using ip

- lets ping b2 from b1 using name

- Lets create two containers in custom bridge
docker run -d --name c1 --net my_bridge alpine sleep 1d
docker run -d --name c2 --net my_bridge alpine sleep 1d
- Lets get ip address of c1 and c2
docker network inspect my_bridge
- we got
10.255.0.2 and 10.255.0.3
-
lets ping c2 from c1 using ip and name
-
IN default bridge network, name resolution is not enabled, where as in custom defined bridge network, name resolution is enabled.
Lets create a database and application.
docker network create -d bridge --subnet 10.200.0.0/24 nop_net
docker volume create nopdb
- Now lets create mysql container with name nopdb
docker run -d \
--name nopdb \
-v nopdb:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root123 \
-e MYSQL_DATABASE=nop \
-e MYSQL_USER=nop \
-e MYSQL_PASSWORD=nop \
--net nop_net \
mysql:8.0
- Now lets run nop server
shaikkhajaibrahim/june-nop
docker run -d \
--name nop \
-P \
--net nop_net \
shaikkhajaibrahim/june-nop
- To simplify executing these many commands docker uses
docker compose
- Refer Here for docker compose referenc
- create a yaml file with name
docker-compose.yml
services:
db:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=root123
- MYSQL_DATABASE=nop
- MYSQL_USER=nop
- MYSQL_PASSWORD=nop
volumes:
- nopdb:/var/lib/mysql
networks:
- nop-net
nop:
image: shaikkhajaibrahim/june-nop
networks:
- nop-net
ports:
- 33000:5000
networks:
nop-net:
volumes:
nopdb:
- We would discuss about compose after yaml.
Exercise:
- I want you to figure out a way to create a container
- with max of 0.5 cores
- with max of 256 MiB RAM
Like this:
Like Loading...