Docker Volumes and Networking
Lab decks 3 and 4: why container data is temporary, Docker volumes, bind mounts, volume vs bind mount, and container networking (default bridge, user-defined bridge, host, macvlan, ipvlan, overlay, none).
Container data is temporarySearch on YouTube
A container has its own writable layer on top of the read-only image layers. Files written inside the container live in that layer.
The lab shows this in four steps:
docker run -d --name demo nginx # pulls nginx:latest if missing, then runs it
docker exec -it demo bash
echo "Hi, This is Jai, Testing the Container" > /tmp/test.txt
cat /tmp/test.txt # prints the line
exitStop and start: the file survives.
docker stop demo
docker start demo
docker exec -it demo bash
cat /tmp/test.txt # still thereRemove and recreate: the file is gone.
docker stop demo
docker rm demo
docker run -it --name demo1 nginx bash
cat /tmp/test.txt # No such file or directoryStopping a container does not delete its data. Removing the container wipes the data with it, because the writable layer belongs to that container.
Real apps produce data that must outlive a container: database files, uploaded images, user documents, logs and other generated files. They need persistent storage outside the container's writable layer.
Docker volumesSearch on YouTube
A volume is storage managed by Docker. Docker creates it, stores it under its own folder on the Docker host, and keeps it after containers are removed.
Create, list, inspectSearch on YouTube
docker volume create vol_data
docker volume ls # DRIVER local, VOLUME NAME vol_data
docker volume inspect vol_datainspect shows JSON with CreatedAt, Driver: "local", Labels, Mountpoint: "/var/lib/docker/volumes/vol_data/_data", Name, Options and Scope: "local". The mountpoint is where the data sits on the Docker host.
Mount a volume into a containerSearch on YouTube
Long form with --mount:
docker run -it --name cont_vol --mount type=volume,source=vol_data,target=/mydata nginx bashShort form with -v volume:path:
docker run -it --name cont_vol2 -v vol_data:/mydata nginx bashIn PowerShell the same command can be split over lines with a backtick:
docker run -it `
--name cont_vol2 `
-v vol_data:/mydata `
nginx bashInside the container, ls / now shows a mydata folder.
--mount key | Meaning |
|---|---|
type | volume or bind |
source (or src) | Volume name, or host path for a bind |
target | Path inside the container |
If the volume does not exist, Docker creates it while running the container:
docker run -it --name cont_vol3 -v vol_data1:/mydata1 nginx bash
docker volume ls # vol_data and vol_data1Data survives container removalSearch on YouTube
# inside cont_vol
echo "This text is from Volume" > /mydata/test.txt
exit
docker rm cont_vol
docker run -it --name cont_vol1 --mount type=volume,source=vol_data,target=/mydata nginx bash
cat /mydata/test.txt # This text is from VolumeMany containers, one volumeSearch on YouTube
Start cont_vol1 and cont_vol2, both mounting vol_data. A file written from one is visible in the other at once:
docker start cont_vol1
docker exec -it cont_vol1 bash
cd mydata && echo "This is written from cont_vol1" > test1.txt
docker start cont_vol2
docker exec -it cont_vol2 bash
cat /mydata/test1.txt # This is written from cont_vol1Key pointsSearch on YouTube
- Containers are disposable: container gone, data also gone.
- A volume is persistent; the container is disposable.
- A Docker volume consumes storage on the Docker host.
- Every container on the same Docker host can access the same volume at the same time.
- Volume is not synchronisation across machines. Developer 1 and Developer 2 on different machines do not share a volume.
Bind mountsSearch on YouTube
A bind mount maps an exact host folder into the container. You choose the path; Docker does not create or manage the storage.
The lab runs two nginx containers that serve the same host folder (holding an index.html with "Hello from Bind Mount!"):
docker run -d --name cont_bind1 -p 8001:80 --mount "type=bind,src=.,target=/usr/share/nginx/html" nginxdocker run -d `
--name cont_bind2 `
-p 8002:80 `
-v ${PWD}:/usr/share/nginx/html `
nginx${PWD} is the current folder in PowerShell (use $(pwd) in a Linux shell). Both localhost:8001 and localhost:8002 show the page.
Edit index.html on the host. Both containers show the new content at once, with no rebuild. This is why bind mounts suit development: you edit source code on the host and the container sees it live.
Volume vs bind mountSearch on YouTube
| Docker volume | Bind mount | |
|---|---|---|
| Who manages storage? | Docker | You / host OS |
| You specify | Volume name | Exact host path |
| Example | mydata:/data | C:\project:/data |
| Where is data? | Docker-managed location | Your chosen host folder |
| Easy to browse from host? | Not normally | Yes |
| Container-independent? | Yes | Yes |
| Typical use | Database / app persistent data | Development / source code |
| Docker creates storage? | Yes | No |
With -v, a name (vol_data:/mydata) means a volume. A path (${PWD}:/usr/share/nginx/html) means a bind mount.
Container networkingSearch on YouTube
Default network typesSearch on YouTube
docker network ls| NAME | DRIVER | SCOPE |
|---|---|---|
| bridge | bridge | local |
| host | host | local |
| none | null | local |
Docker creates these three networks on install.
1. Default bridgeSearch on YouTube
Containers join the default bridge network unless told otherwise.
docker create --name cont1 --network bridge busybox
docker create --name cont2 --network bridge busybox
docker create --name cont3 --network bridge nginx
docker exec -itd cont1 shThe Docker engine creates a virtual switch, docker0, with gateway 172.17.0.1 on subnet 172.17.0.0/16. Each container gets a virtual Ethernet (veth) interface and an IP: cont1 172.17.0.2, cont2 172.17.0.3, cont3 172.17.0.4. Traffic to the internet leaves through the host's interface using NAT.
docker inspect network bridgeThe output shows Driver: "bridge", EnableIPv4: true, EnableIPv6: false, and under IPAM.Config the Subnet: "172.17.0.0/16" and Gateway: "172.17.0.1". The Containers section lists each container's name, MAC address and IPv4Address.
Inside cont1 (ip a shows eth0 with 172.17.0.2/16):
ping 172.17.0.3 # works
ping 172.17.0.4 # works
ping google.com # works: the bridge reaches the internet
ping cont2 # ping: bad address 'cont2'In the default bridge, DHCP is enabled (containers get IPs automatically) but DNS is not. Containers can reach each other by IP, not by name. The host machine also cannot reach the containers in the bridge directly.
2. User-defined bridgeSearch on YouTube
# creating a user defined network (default driver is bridge)
docker network create --driver bridge mynetwork
docker run -itd --name cont1 --network mynetwork busyboxdocker network ls now lists mynetwork with driver bridge. docker inspect network mynetwork shows a new subnet 172.18.0.0/16 with gateway 172.18.0.1.
Connect the existing containers and start them:
docker network connect mynetwork cont1
docker network connect mynetwork cont2
docker network connect mynetwork cont3
docker start cont1 cont2 cont3They get 172.18.0.2, 172.18.0.3 and 172.18.0.4. Now DNS works:
ping cont2 # PING cont2 (172.18.0.3)
ping cont3 # PING cont3 (172.18.0.4)Multiple interfacesSearch on YouTube
- Stopping and starting a container does not disconnect it from the default bridge it joined before.
docker network connect mynetwork cont1adds another network interface formynetwork.- A container can have several network interfaces, so it can be on several Docker networks at once.
ip a inside cont1 shows eth0@if20 with 172.17.0.2/16 (default bridge) and eth1@if21 with 172.18.0.2/16 (mynetwork).
| Command | Effect |
|---|---|
docker run --network mynetwork ... | Connects the container only to mynetwork; the default bridge is not touched |
docker network connect mynetwork cont1 | Adds mynetwork to cont1 alongside any existing networks |
docker network disconnect bridge cont1 | Removes cont1 from the default bridge |
docker network disconnect bridge cont1
docker network disconnect bridge cont2
docker network disconnect bridge cont3After this, ip a in cont1 shows only eth1 (172.18.0.2/16). cont1 is connected only to mynetwork.
Bridge isolationSearch on YouTube
The default bridge network and a user-defined bridge network are isolated from each other. Containers on one cannot reach containers on the other.
3. Host networkSearch on YouTube
docker run -itd --network host --name cont4 nginx
docker exec cont4 sh
docker exec -it cont4 shThe container shares the host's network namespace directly. It gets no private container IP and sees all the host's interfaces (for example eth0, docker0). The host can reach this container. This is easier to see on native Linux than on Docker Desktop, where "host" means the WSL 2 VM.
4. MacvlanSearch on YouTube
Each container gets its own MAC address and IP on the physical LAN. It is like plugging the container straight into the router.
sudo docker network create -d macvlan \
--subnet <your VM subnet/n> \
--gateway <your VM gateway> \
-o parent=<VM NIC interface> \
mymacvlan
sudo docker run -itd --network mymacvlan --ip <assign new ip> --name cont1 busyboxThe host NIC must have promiscuous mode enabled (on the Ubuntu VM and its adapter) so it accepts frames for the extra MACs.
Drawback of macvlan: many MAC addresses appear on one switch port. Some networks block this, and the containers then fail to reach the gateway.
5. IPvlanSearch on YouTube
Containers share the parent interface's MAC address but get their own IPs. No promiscuous mode is needed. The deck shows the containers and the host machine can now interact.
sudo docker network create -d ipvlan \
--subnet <subnet> \
--gateway <gateway> \
-o parent=<link to VM eth interface> \
my_ipvlan6. OverlaySearch on YouTube
Overlay networks are for a Swarm of Docker containers spread across several hosts. The lab does not use them.
7. NoneSearch on YouTube
A completely isolated container. No IP is assigned and it has no network interface except loopback. Best for sandboxing.
Big Uncles Hate My Incredibly Odd Nephew.
The seven network types in order: Bridge (default), User-defined bridge, Host, Macvlan, IPvlan, Overlay, None.
| Type | IP from | DNS by name | Isolation | Use |
|---|---|---|---|---|
| Default bridge | 172.17.0.0/16 | No | From host and other bridges | Quick tests |
| User-defined bridge | New subnet (172.18.0.0/16) | Yes | From default bridge | Multi-container apps |
| Host | Host's own | n/a | None (shares host stack) | Performance, host access |
| Macvlan | LAN subnet, own MAC | n/a | On the LAN like a device | Appear as physical device |
| IPvlan | LAN subnet, shared MAC | n/a | On the LAN | When MACs are limited |
| Overlay | Cluster-wide | Yes | Across hosts | Docker Swarm |
| None | None | n/a | Full | Sandboxing |
What to do and check in the lab:
- Write a file in a plain container. Show it survives
stop/startbut notrm. - Create
vol_data, inspect its mountpoint, mount it with both--mountand-v, write/mydata/test.txt, remove the container and read the file from a new one. - Mount the same volume in two containers and show a file written by one appears in the other.
- Run two nginx containers on ports 8001 and 8002 with a bind mount of the current folder. Edit
index.htmlon the host and refresh both. - Put three containers on the default bridge, inspect the subnet, ping by IP (works) and by name (fails).
- Create
mynetwork, connect the containers, ping by name (works). Check two interfaces withip a, then disconnect frombridge. - Run nginx with
--network host.
Quick self-testSearch on YouTube
Viva: a container was stopped and started again. Is the data written inside it lost? What if it was removed?
Stopping keeps the data, because the writable layer stays with the container. Removing the container deletes the writable layer, so the data is lost.
What is a Docker volume and where is its data stored?
Storage created and managed by Docker that outlives containers. On the Docker host it lives under /var/lib/docker/volumes/<name>/_data, shown as Mountpoint in docker volume inspect.
Write two equivalent commands that mount volume vol_data at /mydata in an nginx container.
docker run -it --name cont_vol --mount type=volume,source=vol_data,target=/mydata nginx bash and docker run -it --name cont_vol2 -v vol_data:/mydata nginx bash.
What happens if you mount a volume name that does not exist?
Docker creates the volume automatically while running the container, as with vol_data1 in the lab.
Give four differences between a volume and a bind mount.
Volume: Docker manages it and you give a name. Its data is in a Docker-managed location, and Docker creates the storage. It is used for database or app data. Bind mount: the host OS manages it and you give an exact host path. Its data is in your chosen folder and easy to browse. Docker does not create it. It is used for development source code.
Viva: why does ping cont2 fail on the default bridge but work on mynetwork?
The default bridge gives IPs by DHCP but has no embedded DNS, so names do not resolve. A user-defined bridge has DNS, so container names resolve to their IPs.
What is the subnet and gateway of the default bridge in the lab, and of mynetwork?
Default bridge: subnet 172.17.0.0/16, gateway 172.17.0.1 (docker0). mynetwork: subnet 172.18.0.0/16, gateway 172.18.0.1.
Difference between docker run --network mynetwork and docker network connect mynetwork cont1?
run --network connects the new container only to mynetwork. network connect adds mynetwork as an extra interface alongside the networks the container already has.
How does a host-network container differ from a bridge container?
It shares the host's network namespace. It gets no private IP, sees the host's interfaces, and needs no port mapping.
Which network type suits a sandbox, and why?
none. The container gets no IP and no network interface apart from loopback, so it is completely isolated.
- Container data lives in its writable layer: it survives stop/start but is lost on
rm. - Volumes are Docker-managed (
docker volume create/ls/inspect), stored under/var/lib/docker/volumes, shared by many containers on one host, and outlive containers. - Mount with
--mount type=volume,source=...,target=...or-v name:/path; a missing volume is auto-created. - Bind mounts map an exact host path; edits on the host show live in every container using it.
docker network lsshowsbridge,host,none. Default bridge: 172.17.0.0/16, DHCP yes, DNS no.- User-defined bridge adds DNS by container name and is isolated from the default bridge; containers can join several networks.
- Host shares the host stack; macvlan gives own MAC (needs promiscuous mode); ipvlan shares the MAC; overlay is for Swarm; none is fully isolated.