Docker networking
- docker network subcommand has options to deal with docker network
-
docker has following network drivers
-
The default network driver is bridge, i.e. when you create containers the contianer will be part of default docker network
-
The default bridge network allows containers to communicate over ip but name resolution cannot be done
-
The name resolution is possible when you create your own bridge network
-
Host driver means your container gets an ip of the host
-
Generally when we create a network you can also specify network range
-
Docker also supports multi host network using docker swarm and the network driver will be overlay.
Scenario
-
I have the following application
-
The idea is to run this application in containers
- webstore => reactjs application
- storeapi => fastapi
- db => postgresdb
-
We need to build docker images for
-
We need to ensure the containers can communicate with each other which requires us to create network
- Generally identifying by name is more sensible
-
We have a database i.e. it will store data which requires us to deal with volumes
-
Steps:
- create network
- create volume
- launch containers one by one
- Roughly the commands to create
docker network create -d bridge store_net
docker volume create store_vol
docker run -d -v store_vol:/var/lib/psql --name storedb --network store_net postgres:19
docker run -d --name storeservice --network store_net storeservice:1.0
docker run -d --name webstore -P --network store_net webstore:1.0
- Now if we get new code changes
# rebuild images
docker image build -t storeservice:1.1 src/service
docker image build -t webstore:1.1 src/frontend
docker rm -f webstore storeservice
docker run -d --name storeservice --network store_net storeservice:1.1
docker run -d --name webstore -P --network store_net webstore:1.1
-
Refer Here for additional networking references
-
Exercise: Create contianers with specific cpu and ram Refer Here