Docker Volumes
- Official docs
- Volumes are persistent data stores which have a different lifetime than containers.
- We can choose the directory in our container & map it to the volume
- now even if you delete the container the particular content in directory remains as a docker volume

- Docker has 2 types of volumes
- Bind mount:
- Docker managed volumes
- Folder sync between container & host
- tempfs mount: It mounts the data into memory of the container.
- Bind mount:
- docker volume also supports drivers to store the volumes in aws/azure/nfs etc
- If you are creating a Dockerfile for a stateful application it is recommended to include the VOLUME in the Dockerfile
-
Application Types:
- Stateless: They use external systems if they have to preserve state.
- Stateful: Which uses local storage to preserve some state or data. Examples (databases, etc)
- Watch classroom recording for commands
Interaction between containers – Networking
- To understand docker networking read following articles
- docker has set up a Container Networking Model (CNM) to implement networking in containers.
- Note: K8s follows CNI (Container Networking Interface) standard
-
CNM is a specification
-
CNM is all about
- Sandbox
- Endpoint
- Network
- Network is implemented by drivers
- When a container is created first a Sandbox (Network namespace is created) which gets an endpoint (ipaddress) after connecting to a Network
- By default in docker (linux) host we get following drivers
- bridge: this is default network. This enables communication within the network and also bridge for host to contiainer communication. Default bridge network cannot resolve by names it can resolve only by ips. If you create your own bridge network we can resolve by names as well.
- host: host network will be assigned to container
- None: container will be isolated with no network
- overlay: for multiple hosts
- In addition to this we can use
- macoverlan
- ipoverlan
- NAT (default for windows contianers)
Dockerfile for a .net application with a database
- Lets try building a image for nopCommerce
- For building .net applications we need sdk and to run the dotnet application we need runtime images
- Refer Here for the changes done.
- Refer Here for fixes related to user
- NOw we have a image
nop:1 - Lets create a new bridge network
docker network create -d bridge --subnet 10.10.0.0/24 nop-net
- Now create a volume nop-vol
docker volume create nop-vol
- Now lets create a mysql container with the following config and in nop-net with name
nop-db- user: nop
- password: nop
- rootpassword: nopnop
- initial database: nopdb
docker container run -d -e "MYSQL_ROOT_PASSWORD=nopnop" \
-e "MYSQL_DATABASE=nopdb" \
-e "MYSQL_USER=nop" \
-e "MYSQL_PASSWORD=nop"\
--name nop-db \
-v "nop-vol:/var/lib/mysql" \
--network "nop-net" \
mysql:9

* Now create nop container in the same network
docker container run -d \
-p 5000:5000 \
--network "nop-net" \
--name nop1 \
nop:1
- lets examine the resource utilization
docker stats

*
