πŸš€ 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 if cd nginx-volume shows permission denied

  • Remove 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 TypeBest ForReal-World Analog
bridgeIsolated container networksHome Wi-Fi Router
user-defined bridgeCustom app networksOffice LAN
hostHigh performance appsUsing your laptop directly
overlaySwarm servicesVPN over multiple networks
macvlanStatic IP for containerVirtual Network Interface
ipvlanLightweight IP sharingShared Ethernet Card
noneComplete isolationNo 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 to Anywhere

Check app:

<public-ip>:5000

Now the containers are communicating properly!

Default ( bridge ) Networks vs. User-Defined ( bridge ) Networks

FeatureDefault Networks (bridge, host, none)User-Defined Networks bridge, macvlan, ipvlan, overlay)
CreationAuto-created by DockerManually created by users (docker network create)
NamingPredefined names (bridge, host, none)Custom names (e.g., my_app_net)
DNS Resolution❌ No automatic DNSβœ… Containers can ping each other by name
IsolationAll containers share the default bridgeIsolated network segments for different apps
IP AssignmentRandom IPs (unless specified)βœ… Custom subnets/IPs (e.g., --subnet=10.5.0.0/16)
Use CasesBasic testing/developmentProduction apps, multi-service architectures
Network DriversOnly bridge, host, noneSupports bridge, macvlan, ipvlan, overlay
SecurityLess 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 to mysql using mysql 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: Use

      sudo chown -R $USER:$USER mysql-volume
    
  • buildx error: Use

      docker 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! πŸ’ͺ


0
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!