π Docker Advance Guide for DevOps Learners ( Part 3 )


Welcome to Part 3 of our Docker series! In this blog, we dive deeper into advanced Docker concepts including Volumes, Networks, and Docker Compose with multi-stage builds. If you're building real-world applications with Docker, this guide will help you organize and scale like a pro! π§±
π Docker Volumes
β What is a Docker Volume?
A Docker volume is a persistent storage mechanism. It allows container data to survive restarts, and even deletion of the container. It acts as a bridge between the container and the host filesystem.
"Volume = Backup container data to host."
π Example with MySQL
Step-by-step:
mkdir mysql-volume
pwd # Copy this path
Run MySQL container:
docker run -d \
-v /home/ubuntu/mysql-volume:/var/lib/mysql \
--name mysql \
-e MYSQL_ROOT_PASSWORD=Test@123 \
mysql:latest
Verify container:
docker ps
Enter container and start MySQL:
docker exec -it <container_id> bash
mysql -u root -p
Now inside MySQL:
CREATE DATABASE kyc_devops;
USE kyc_devops;
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
INSERT INTO messages (message) VALUES ("kyc submitted");
SELECT * FROM messages;
Check volume data:
cd mysql-volume
ls
πͺ€ Volume Explanation
-v host_path:/container_path
Host path
/home/ubuntu/mysql-volume
maps to/var/lib/mysql
inside container
π Volume Use Cases
Data persistence for DBs
Share data between containers
π Logical Named Volume
docker volume create mysql-volume
Inspect:
docker volume ls
docker volume inspect mysql-volume
Mount named volume:
docker run -d \
-v mysql-volume:/var/lib/mysql \
--name mysql \
-e MYSQL_ROOT_PASSWORD=Test@123 \
mysql:latest
Same for NGINX:
docker run -d -v nginx-volume:/var/lib/ -p 80:80 nginx:latest
π Port Mapping
# Format: host_port:container_port
-p 80:80
π Volume Errors:
Use
sudo su
ifcd nginx-volume
shows permission deniedRemove volume:
docker volume rm nginx-volume
π Docker Networking
β Definition
Docker networking allows containers to communicate securely and efficiently across different environments.
πͺ€ Types of Networks
Network Type | Best For | Real-World Analog |
bridge | Isolated container networks | Home Wi-Fi Router |
user-defined bridge | Custom app networks | Office LAN |
host | High performance apps | Using your laptop directly |
overlay | Swarm services | VPN over multiple networks |
macvlan | Static IP for container | Virtual Network Interface |
ipvlan | Lightweight IP sharing | Shared Ethernet Card |
none | Complete isolation | No network cable plugged in |
π User Defined Bridge Network
Create:
docker network create -d bridge twotier
Run MySQL container:
docker run -d --name mysql \
-v mysql-volume:/var/lib/mysql \
-e MYSQL_DATABASE=mydb \
-e MYSQL_ROOT_PASSWORD=Test@123 \
-p 3306:3306 \
--network=twotier \
mysql:latest
Run Flask app:
docker run -d --name flaskapp \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_ROOT_PASSWORD=Test@123 \
-e MYSQL_DB=mydb \
-p 5000:5000 \
--network=twotier \
flaskapp:latest
Inspect network:
docker network inspect twotier
β Both containers should appear under the
Containers
section β showing they're linked.
π Final Step - EC2 Access
Go to:
AWS Console > EC2 > Security Groups > Edit Inbound Rules
Add port
5000
and set Source toAnywhere
Check app:
<public-ip>:5000
Now the containers are communicating properly!
Default ( bridge ) Networks vs. User-Defined ( bridge ) Networks
Feature | Default Networks (bridge , host , none ) | User-Defined Networks bridge , macvlan , ipvlan , overlay ) |
Creation | Auto-created by Docker | Manually created by users (docker network create ) |
Naming | Predefined names (bridge , host , none ) | Custom names (e.g., my_app_net ) |
DNS Resolution | β No automatic DNS | β Containers can ping each other by name |
Isolation | All containers share the default bridge | Isolated network segments for different apps |
IP Assignment | Random IPs (unless specified) | β
Custom subnets/IPs (e.g., --subnet=10.5.0.0/16 ) |
Use Cases | Basic testing/development | Production apps, multi-service architectures |
Network Drivers | Only bridge , host , none | Supports bridge , macvlan , ipvlan , overlay |
Security | Less isolated (shared default bridge) | More secure (isolated networks) |
β
Example of Default bridge
Network
bashCopyEditdocker run -d --name container1 nginx
docker run -d --name container2 busybox
Both are in
bridge
network.To ping: use IP address (e.g.
ping 172.17.0.2
) β not container name.
β
Example of user-defined bridge
Network
bashCopyEditdocker network create mynetwork
docker run -d --name mysql --network=mynetwork mysql
docker run -d --name flaskapp --network=mynetwork flaskapp
Now
flaskapp
can talk tomysql
usingmysql
as hostname. βBetter for Docker Compose, Microservices, and DevOps setups.
π‘ Summary
β User-defined networks = Want name-based communication, better control, and production-readiness + More control, DNS, isolation, security.
β Default networks = Want easy setup but limited features + Simple but limited (no DNS, shared IP space).
π Docker Prune
π Use Case
To clean unused Docker objects like stopped containers, unused volumes, and networks.
π Commands:
docker system prune
# Clean everything
docker volume prune
# Clean unused volumes
docker network prune
# Clean unused networks
docker image prune
# Clean dangling images
π Docker Compose:
β Meaning
Docker Compose allows defining and running multi-container applications using a YAML file.
π Installation (Linux)
sudo apt-get update
sudo apt-get install docker-compose-plugin
π Flask App Compose File
version: "3.8"
services:
mysql:
image: mysql:latest
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: Test@123
MYSQL_DATABASE: mydb
volumes:
- mysql-volume:/var/lib/mysql
networks:
- twotier
flaskapp:
build:
context: .
dockerfile: Dockerfile
container_name: flaskapp
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: Test@123
MYSQL_DB: mydb
ports:
- "5000:5000"
networks:
- twotier
volumes:
mysql-volume:
networks:
twotier:
get code/file from:
git clone git@github.com:abhishek26w/Docker.git
& click on day-2
π Commands:
docker compose config
# Validate YAML file
docker compose up
# Start all services
docker compose up -d
# Detached mode
docker compose down
# Stop and remove all
π Multi-Stage Dockerfile β Optimize Your Images
β Definition
Break image creation into stages β build in one, run in another β smaller and cleaner images.
Multi-stage = Clean build + lightweight runtime π
π Example: Python Flask App
# Stage 1 - Build
FROM python:3.10-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
COPY . .
# Stage 2 - Run
FROM python:3.10-slim
WORKDIR /app
COPY --from=builder /app /app
CMD ["python", "app.py"]
β οΈ Common Errors
permission denied from mysql-data
: Usesudo chown -R $USER:$USER mysql-volume
buildx error:
Usedocker buildx install
π¨ Final Steps
sudo curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
newgrp docker
Check:
docker version
docker compose version
π Summary
π¦ Docker Volume
π Docker Network
π§© Docker Compose
π Multi-Stage Builds
Boost your DevOps power by mastering these tools! πͺ
Subscribe to my newsletter
Read articles from ABHISHEK WAGHMARE directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

ABHISHEK WAGHMARE
ABHISHEK WAGHMARE
An IntroductionβTo DevOps: Where Development And Operations Meet π My DevOps learner journey has been inspired by a true passion for continual personal development and a genuine curiosity forβcloud and automation technologies. With the practice of engaging in numerous online coursework and community threads, I have built a growing comprehension of what is necessary for everyday life in the toolsβoffered from Docker, Jenkins, and Kubernetes, which are mandatories in the IT Society. π What sets me apart? A commitment to practical application. Through personal projects, I actively implement my learning to solve real-world problems, gaining hands-on experience. This proactive approach helps me not only understand technologies at a surface level but to deeply integrate them into effective solutions. My ultimate goal? To merge innovative DevOps practices with business objectives to streamline operations and boost productivity in any tech landscape. I am eager to bring my fresh perspective and evolving expertise to a vibrant team, where continuous learning is intertwined with company growth. π¨ Letβs connect and explore how we can drive progress together in the fascinating world of DevOps!