Docker Volumes and Networking, Hands-On
You run a database in a container, add data, then remove the container to start a fresh one: the data is gone. And your web app, in a second container, cannot even find that database. Both problems have the same answer: Docker volumes and networking. This lesson shows how to keep data outside a container and let containers reach each other by name.
Two problems: files that vanish, boxes that cannot talk
As Docker Image Layers showed, a container writes into its thin writable layer, which docker rm deletes with the container. That is fine for scratch files but not for a database, and containers are removed often: every upgrade replaces the container.
The second problem is isolation. Each container has its own network namespace, the kernel feature from lesson 1 that gives it private network interfaces, IP addresses and ports. That prevents clashes, but two containers then need a deliberate way to find each other.
Volumes and bind mounts keep data outside
The fix for vanishing files is a mount: storage from outside the container, attached at a folder inside it called the mount point. Writes under that path skip the writable layer and land in the outside storage, which survives docker rm.
A volume is storage that Docker creates and manages. A named volume has a name you choose, such as db-data, so a later container can mount it again. Docker Engine on Linux stores it under /var/lib/docker/volumes; Docker Desktop keeps it inside its hidden Linux VM.
A bind mount maps a folder or file you choose on your machine into the container. Both sides see the same files, so an edit on your laptop shows inside the container at once. That suits code and config, but ties the container to your folder layout.
In the diagram, notice that the app just writes to /data, unaware that the files live outside.
Using them: -v, --mount and read-only
You attach a mount with the short -v flag, one string of source and target plus options after a second colon, or the longer --mount, which names each part. A source without a slash is a volume name, and Docker creates that volume on first use. These two are the same:
-v db-data:/data
--mount type=volume,source=db-data,target=/data
A typical use is a database's data folder, as in the demo below. A mount can also be read-only, so the container can read its files but never change them: add :ro to -v, or readonly to --mount.
To try a read-only bind mount, use nginx from lesson 4, which serves files from /usr/share/nginx/html. In a terminal on macOS or Linux, make a folder with one page and mount it there; $(pwd) is the current folder's full path, and -d runs the container in the background:
mkdir site
echo 'Served from my folder' > site/index.html
docker run -d --name page -p 127.0.0.1:8081:80 -v "$(pwd)/site:/usr/share/nginx/html:ro" nginx:1.27-alpine
curl http://localhost:8081
docker run -d prints the container's long ID. Then curl should print your page, not the nginx welcome page, which the mount hides:
Served from my folder
Edit the page and curl again: the change shows at once. A 403 Forbidden means nginx found an empty folder: -v quietly creates a missing host path as an empty folder, whereas --mount stops with an error. Thanks to :ro, writing inside fails: docker exec page touch /usr/share/nginx/html/x, which runs touch in the container, reports Read-only file system. Clean up with docker rm -f page.
The flowchart sums up the choice.
Ports, localhost and the default bridge
A Docker network is a private virtual network that containers join, each with its own IP address. The usual kind is a bridge network: a software switch on the host that links its containers to each other and, through the host, to the internet.
Docker creates a default one named bridge, and every container started without --network joins it, usually with an address such as 172.17.0.2. There, containers can reach each other only by IP address, which may change when a container is recreated.
Inside a container, localhost (127.0.0.1) is the container itself, not your laptop or another container. So a web app looking for its database at localhost:6379 finds nothing.
Traffic from your machine comes in through a published port, as in Dockerfile Basics: -p 127.0.0.1:8080:8000 forwards your port 8080 to the container's port 8000, host side first. Containers on one network need no published port to reach each other.
User-defined networks: call containers by name
A user-defined bridge network is one you create with docker network create. Its big extra is Docker's built-in DNS server, the service that turns names into IP addresses. A container named db is reachable as db from any container on the same network, while containers outside the network cannot reach it.
Now combine both ideas: extend lesson 3's Flask app to count visits in Redis, a small, fast key-value database, running as db with its data on db-data. Notice in the diagram that only web publishes a port.
First create the network and start Redis. As in lesson 3, words after the image name replace its start command; these make Redis log every change to a file in /data, the volume.
docker network create app-net
docker run -d --name db --network app-net -v db-data:/data redis:7.4-alpine redis-server --appendonly yes
Each command prints a long ID; if the name is already in use, remove the old container with docker rm -f db. Then, in your myapp folder, add redis==5.2.1, the Python library for Redis, to requirements.txt, and change app.py to:
import redis
from flask import Flask
app = Flask(__name__)
db = redis.Redis(host="db", port=6379)
@app.route("/")
def hello():
visits = db.incr("visits")
return f"Hello from my container, visit {visits}\n"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
host="db" is the container's name, which Docker's DNS resolves. incr adds 1 to the key visits and returns the new count. Rebuild, which reruns the install step, and start the web container on the same network:
docker build -t myapp .
docker run -d --name web --network app-net -p 127.0.0.1:8080:8000 myapp
Give it a second to start, then run curl http://localhost:8080 twice. The count should go up:
Hello from my container, visit 1
Hello from my container, visit 2
If curl shows Internal Server Error, check docker logs web: a ConnectionError for db:6379 usually means one container lacks --network app-net. Now prove the data outlives its containers: stop the database so Redis shuts down cleanly, then delete both.
docker stop db
docker rm -f web db
docker volume ls
The volume list still shows db-data. Rerun both docker run commands as before, wait a second, and curl again. You should see:
Hello from my container, visit 3
The count carried on because it lived in the volume. To clean up, run docker rm -f web db, docker network rm app-net and docker volume rm db-data, which deletes the data for good. Docker Compose Basics turns these long commands into one file.
Check yourself
An app saves uploads in /app/uploads with no mount. You replace its container with one from a newer image. Where are the uploads?
Gone: they were in the old container's writable layer, which docker rm deleted. A named volume at /app/uploads would have kept them.
Your web container tries localhost:6379 to reach Redis in db and gets "connection refused". Why?
Inside the web container, localhost is that container itself, where no Redis runs. Put both on a user-defined network and connect to db:6379.
Must you publish Redis's port with -p for web to reach db?
No. -p only lets in traffic from outside Docker's networks; containers on one network reach each other's ports directly, so publishing would only expose the database.