Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔍 Overview
After mastering Docker images and containers in the previous
chapters, you're now ready to tackle two vital concepts that power real-world
containerized applications:
This chapter teaches you how to manage container
communication and ensure long-term data storage using volumes and mounts.
🌐 Part 1: Docker
Networking
🚀 Why Networking Matters
in Docker
Containers are often part of a larger, multi-service
ecosystem (e.g., app + database + cache). Docker provides flexible networking
options so containers can:
🔌 Docker Network Drivers
Docker includes multiple built-in network drivers,
each serving different purposes.
Network Driver |
Use Case |
bridge |
Default for standalone
containers |
host |
Uses host
machine’s network stack |
none |
No networking at all
(fully isolated) |
overlay |
Multi-host
networking (requires Swarm) |
macvlan |
Assign MAC addresses
to containers |
🧱 Bridge Network
(Default)
When you run a container without specifying a network, it
connects to the bridge network by default.
bash
docker
network ls
bash
docker
inspect bridge
Containers on the bridge network can talk to each other
using IP addresses (or container names if in the same user-defined network).
🛠️ Creating a
User-Defined Network
bash
docker
network create my-network
Run containers inside it:
bash
docker
run -d --name app1 --network my-network nginx
docker
run -d --name app2 --network my-network httpd
Now app1 can ping app2 by container name:
bash
docker
exec -it app1 ping app2
🔄 Port Binding: Accessing
Containers from Outside
Use the -p flag to bind container ports to host ports:
bash
docker
run -d -p 8080:80 nginx
Port Mapping |
Purpose |
8080:80 |
Host port 8080
forwards to container port 80 |
Visit http://localhost:8080 in your browser to test.
🧪 Host Network
On Linux, you can use the host's network stack:
bash
docker
run --network host nginx
Useful for performance-critical scenarios but sacrifices
container isolation.
❌ Isolated Containers (None
Network)
bash
docker
run --network none alpine
No network access. This is ideal for security testing or
resource-restricted environments.
💾 Part 2: Docker Data
Persistence
By default, Docker containers are ephemeral—any data
inside a container disappears when it’s deleted. But most applications need
persistent storage (e.g., databases, logs, configs).
Docker solves this using:
🧱 Docker Volumes
Volumes are managed by Docker and stored in
/var/lib/docker/volumes.
🔨 Create a Volume:
bash
docker
volume create mydata
🧪 Use Volume in a
Container:
bash
docker
run -d -v mydata:/app/data nginx
This mounts the volume mydata into /app/data inside the
container.
🧾 Listing and Inspecting
Volumes
bash
docker
volume ls
docker
volume inspect mydata
🗑️ Remove a Volume:
bash
docker
volume rm mydata
Volumes are not deleted automatically when a container is
removed unless --rm is used with --mount.
🗂️ Bind Mounts
Bind mounts map a host directory to a container path.
bash
docker
run -v /home/user/web:/usr/share/nginx/html nginx
Element |
Value |
Host Path |
/home/user/web |
Container Path |
/usr/share/nginx/html |
Useful for:
📊 Comparison Table:
Volumes vs. Bind Mounts
Feature |
Volume |
Bind Mount |
Managed by Docker |
✅ Yes |
❌ No |
Host Path Needed |
❌
No |
✅
Yes |
Best For |
Production environments |
Local development |
Backup/Restore |
Easier |
Manual |
Portability |
More portable |
Less portable |
📁 tmpfs Mounts
Temporary, in-memory storage:
bash
docker
run --tmpfs /app/cache nginx
Data is lost when the container stops. Good for sensitive
data or caching.
🧪 Practical Use Case:
Containerized MySQL with Volume
bash
docker
volume create mysql-data
docker
run -d \
--name mydb \
-e MYSQL_ROOT_PASSWORD=rootpass \
-v mysql-data:/var/lib/mysql \
mysql:5.7
🧰 Useful Volume Commands
Command |
Description |
docker volume ls |
List volumes |
docker volume inspect <vol> |
View volume
details |
docker volume rm
<vol> |
Delete volume |
docker volume prune |
Remove unused
volumes |
🧹 Clean-up Commands
bash
docker
network prune
bash
docker
volume prune
bash
docker
system prune -a --volumes
🔐 Security Tip
✅ Summary of Chapter 3
You’ve learned:
This knowledge is essential for building real,
production-ready containerized applications.
Answer: Docker is a containerization platform that
allows developers to package applications and their dependencies into isolated
units called containers. It ensures consistency across different environments,
speeds up deployment, and makes application scaling easier.
Answer: Containers share the host system’s OS kernel,
making them lightweight and fast, while VMs run a full guest OS, making them
heavier and slower. Containers are ideal for microservices and rapid
deployment, whereas VMs are better suited for full OS-level isolation.
Answer: While basic knowledge of Linux command-line
tools is helpful, it’s not mandatory to start with Docker. Docker also works on
Windows and macOS, and many beginner tutorials (including this one) walk you
through all required commands step-by-step.
Answer: A Docker image is a read-only template
used to create containers, while a Docker container is a running
instance of an image. You can think of an image as a blueprint and a container
as the building made from it.
Answer: You can download Docker Desktop for Windows
or macOS from https://www.docker.com,
or install Docker Engine on Linux using your distro’s package manager (like
apt, yum, or dnf).
Answer: A Dockerfile is a script that contains a set
of instructions for building a Docker image. It typically includes a base
image, environment setup, file copying, and the command to run when the
container starts.
Answer: Docker Hub is a cloud-based repository where
users can share and store Docker images. It has free tiers and allows you to
download popular open-source images or push your own images to share with
others or use in CI/CD pipelines.
Answer: Yes, you can run multiple containers
simultaneously. Tools like Docker Compose even allow you to define and manage
multi-container applications using a simple YAML configuration file.
Answer: You can use volumes or bind mounts
to persist data outside the container’s lifecycle. This allows your application
data to survive container restarts or recreations.
Answer: Docker offers many security benefits like
container isolation and image scanning. However, security also depends on your
image sources, proper configurations, and updates. It's important to follow
Docker security best practices for production deployments.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)