Docker for Beginners: A Hands-On Tutorial to Master Containers from Scratch

1.36K 0 0 0 0

✅ Chapter 3: Docker Networking and Data Persistence

🔍 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:

  • Docker Networking: Enabling communication between containers, hosts, and the outside world.
  • Docker Data Persistence: Ensuring important data survives even when containers are removed.

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:

  • Communicate with each other
  • Be isolated for security
  • Expose services to the outside world

🔌 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:

  • Volumes
  • Bind Mounts
  • tmpfs Mounts

🧱 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:

  • Local development (live file changes)
  • Sharing logs or configs between host and container

📊 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

  • Ensures MySQL data persists across container restarts.
  • Volume mysql-data stores database files securely.

🧰 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

  • Prune unused networks:

bash

 

docker network prune

  • Prune unused volumes:

bash

 

docker volume prune

  • Prune all unused resources:

bash

 

docker system prune -a --volumes


🔐 Security Tip

  • Never mount sensitive directories like /etc, /root, or /var/lib/docker unless you know exactly what you're doing.
  • Use volumes, not bind mounts, for untrusted applications in production.

Summary of Chapter 3

You’ve learned:

  • The difference between Docker networking types: bridge, host, overlay, none
  • How to connect containers using user-defined networks
  • How port mapping makes services accessible from outside
  • The role of volumes and bind mounts in persisting data
  • Real-world usage of volume-mounting with MySQL


This knowledge is essential for building real, production-ready containerized applications.

Back

FAQs


✅ 1. What is Docker and why should I use it?

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.

✅ 2. What is the difference between a Docker container and a virtual machine (VM)?

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.

✅ 3. Do I need to know Linux to use Docker?

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.

✅ 4. What is the difference between a Docker image and a Docker container?

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.

✅ 5. How do I install Docker on my computer?

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).

✅ 6. What is a Dockerfile and how is it used?

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.

✅ 7. What is Docker Hub and is it free?

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.

✅ 8. Can I run multiple containers at the same time?

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.

✅ 9. How do I persist data in a Docker container?

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.

✅ 10. Is Docker secure?

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.