β¨ Docker Volume :
Docker volumes are like special storage areas managed by Docker for your containers. They're better than using regular folders from your computer because:
π‘οΈ Easy to Backup and Move: Volumes make it easy to save or move data.
π§ Simple to Manage: Docker gives you easy commands to control volumes.
π Works on Any System: Volumes work on both Linux and Windows.
π Safe Sharing: You can safely share data between multiple containers.
β¨ Extra Features: You can do cool things like store data on other computers or encrypt it.
π Start with Data: You can start a volume with some data already in it.
π Better Performance: Volumes work faster than regular folders, especially on Mac and Windows.
Think of volumes as a special storage area managed by Docker, making it easy and safe to handle your container data.
β¨ Docker Network :
Docker helps you run applications in isolated environments called containers. To manage communication between these containers and the outside world, Docker uses different types of networks:
π Bridge Network:
Connects containers to each other and your host.
Containers get their own IP address.
They can communicate with each other but are isolated from other networks.
π Host Network:
Containers use the hostβs network directly.
No separate IP addresses for containers.
Ports used by containers are the same as the hostβs ports.
π Overlay Network:
Connects containers across multiple Docker hosts.
Useful for clusters of Docker hosts (like Docker Swarm).
π‘ IPvLAN Network:
Gives precise control over IP addresses.
Useful for connecting containers to existing networks.
π Macvlan Network:
Makes containers appear as physical devices on your network.
Each container gets its own MAC address.
β Task-1: Create a Multi-Container Docker-Compose File
π Steps:
Create a
docker-compose.yml
file with the following content:version: '3.8' services: web: image: nginx:latest ports: - "80:80" db: image: mysql:latest environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: sample_db volumes: - db_data:/var/lib/mysql volumes: db_data:
Run the following commands:
π Start containers:
docker-compose up -d
π Scale web service to 3 instances:
docker-compose scale web=3
π Check container status:
docker-compose ps
π View logs for web service:
docker-compose logs web
π Stop and remove containers:
docker-compose down
β Task-2: Use Docker Volumes to Share Data Between Containers
π Steps:
π¦ Create a named volume:
docker volume create my_shared_volume
π³ Run two containers with the shared volume:
docker run -d --name container1 --mount source=my_shared_volume,target=/shared_volume alpine tail -f /dev/null docker run -d --name container2 --mount source=my_shared_volume,target=/shared_volume alpine tail -f /dev/null
βοΈ Write data from one container and check it from another:
docker exec container1 sh -c "echo 'Hello from container1' > /shared_volume/data.txt" docker exec container2 cat /shared_volume/data.txt
You should see "Hello from container1" when you check from the second container.
π List all volumes and remove the shared volume:
docker volume ls docker volume rm my_shared_volume
In summary, Docker volumes are great for sharing data between containers, making it easy to manage and keep your data safe.
Happy Learning! π