Networking in Containers
- Refer Here for networking series in docker
- Docker defined a standard for container networking called as Container Networking Model (CNM)
- CNM is the spec and libnetwork is the implementation
Experiment: Interaction between two containers in default bridge
- Examine default bridge
- create a container C1 and C2 in default bridge network
docker container run --name C1 -d alpine sleep 1d
docker container run --name C2 -d alpine sleep 1d
* Now examine default bridge network to find ips of C1 and C2
* Now lets ping C2 from C1 by using ip as well as name
* Two containers in default bridge are able to communicate with each other using ip addresses but not names i.e. name based resolution is not working
* Now create a new bridge network called as learn-net with cidr range 192.168.100.0/24
docker network create -d bridge --subnet "192.168.100.0/24" learn-net
* Now lets create two containers L1 and L2 in the learn-net
docker container run --name L1 --network 'learn-net' -d alpine sleep 1d
docker container run --name L2 --network 'learn-net' -d alpine sleep 1d
- Now ping L2 from L1 using ip and name
docker container exec L1 ping -c4 L2
* In user defined bridge network name based as well ip based communication is enabled
Lets Create nop-commerce application by creating volumes and networks
- Overview
steps
- Create a volume
nop-db
docker volume create nop-db
- Create a network
nopnet
docker network create -d bridge --subnet '10.100.100.0/24' nopnet
- Create a mysql container with username nop, volume, password
nop123
in the nopnet with namenopdb
docker container run -d --name nopdb \
--network nopnet \
-e "MYSQL_ROOT_PASSWORD=nop123" \
-e "MYSQL_USER=nop" \
-e "MYSQL_PASSWORD=nop123" \
-e "MYSQL_DATABASE=nop" \
-v nop-db:/var/lib/mysql \
mysql:8
- Create the nop container on nopnet
docker container run -d --name nop --network nopnet -P nop
* Inspect network
* Now watch the classroom video to bring up the application