Master Docker Data Persistence, Networking, and Resource Limits: Practical Guide
This guide explains Docker's three data persistence methods (volumes, bind mounts, tmpfs), demonstrates practical examples, details the various network modes (bridge, host, none, container, custom), and shows how to apply resource limits such as memory and CPU constraints to containers.
Docker Data Persistence Methods
Docker provides three ways to mount host data into containers: volumes, bind mounts, and tmpfs.
volumes : Docker-managed storage under /var/lib/docker/volumes, recommended for persisting data.
bind mounts : Mount any host directory or file into the container.
tmpfs : Stores data in host memory, not written to disk, useful for non‑persistent data and performance.
docker volume create nginx_vol
ls /var/lib/docker/volumes/nginx_vol/Data is stored in the _data directory.
Example 1 – Using volumes
docker run -d --name nginx-ceshi -p 888:80 --mount -v nginx-ceshi:/usr/share/nginx/html nginx
# or
docker run -d --name nginx-ceshi -p 888:80 --mount src=nginx-ceshi,dst=/usr/share/nginx/html nginx
docker inspect nginx-ceshiAfter creating the container you can see the persisted data directory.
cd /var/lib/docker/volumes/nginx-ceshi/_data
echo "hello world nginx" > index.html
# Access http://<host>:888/ to verify persistenceExample 2 – Bind mounts
docker run -d -it --name=nginx-test --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html/ nginx
docker run -d -it --name=nginx-test -v /root/html:/usr/share/nginx/html/ nginxNotes: -v with an absolute path creates a bind mount; -v with a name creates a volume. Bind source must exist; the container’s mount point hides existing files.
Docker Network Modes
bridge
-net=bridge – default mode, Docker creates a docker0 bridge and attaches containers.
host
-net=host – container shares the host’s network namespace, no separate network interface.
none
-net=none – container gets an isolated network namespace but no network configuration; user must configure manually.
container
-net=container:Name/ID – shares the network namespace of another container while keeping other namespaces isolated.
Example:
docker run -itd --name zd -p 99:80 busybox
docker run -itd --name nginx-zd --net container:zd nginx
curl 192.168.106.100:99 # nginx uses zd’s networkCustom networks behave like bridge but provide internal DNS for container‑to‑container name resolution.
docker network create zf-100Docker Resource Limits
Docker can limit container resources to prevent exhausting the host and to improve isolation.
Common options:
-m, --memory Max memory the container can use
--memory-swap Amount of memory+swap allowed
--oom-kill-disable Disable OOM killer
--cpus Number of CPUs the container can use
--cpuset-cpus Specific CPU cores (e.g., 0-3)
--cpu-shares Relative CPU weightExamples:
docker run -d --name web03 --memory="500m" --memory-swap="600m" --oom-kill-disable nginx
docker stats --no-stream web03 # view memory usage docker run -d --name web01 --cpus="1" nginx # limit to one CPU
docker run -d --name web01 --cpus=".5" nginx # limit to 50% CPUSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
