Day 19 Guide to Docker Volume and Network Setup
Hey DevOps enthusiasts! ๐ Ever wondered how Docker manages data or how containers communicate with each other? If yes, you're in the right place! Today, weโre diving into Docker Volumes and Docker Networks in the most practical way possible. Plus, weโve got two fun hands-on tasks to help you get your hands dirty with Docker! ๐ ๏ธ
What is a Docker Volume?๐ค
Imagine youโre running a cafรฉ โ where customers place orders, but every time they leave, you lose track of those orders. Thatโs how a container behaves with its dataโonce it stops, the data inside it is gone. ๐ฑ
Docker Volume is like a notepad ๐ where you can write those orders. Even if the cafรฉ closes (container stops), the notepad (volume) remains, preserving the data for future use. ๐
Why use it? Because data in containers is temporary โ once the container stops, the data disappears. Volumes help you store data permanently! ๐
In simple terms:
Docker Volume is a storage mechanism used to save data that persists even when the container is stopped or removed.
Why Use Docker Volumes?
Data persistence: Keep your data even after the container is gone.
Data sharing: Share data between multiple containers.
Data backups: Easily back up important files.
What is a Docker Network?๐
Now, imagine your cafรฉ has two sectionsโone for customers (frontend) and one for chefs (backend). They need to talk to each other! Docker Network works as the intercom ๐ that lets them communicate.
In technical terms:
Docker Network allows containers to communicate with each other and with the outside world.
Why Use Docker Networks?
Isolate services: Keep your containers secure and isolated.
Simplify communication: Allow containers to talk without exposing them to the internet.
Custom networks: Set up specific networks for different environments.
๐ Prerequisites ๐
Make sure you have:
Before diving into the tasks, ensure you have:
Docker and Docker Compose installed on your system.
Basic Understanding of Docker commands and YAML files.
A code editor like Vim, Nano, or VS Code to edit your
docker-compose.yml
.
Letโs Get Hands-On! ๐ช
๐๏ธ Task 1: Create a Multi-Container Docker-Compose Setup
Goal:
We'll set up a web application and a database in separate containers, connected via a network, with persistent data using volumes. ๐
Letโs build a simple multi-container application with Docker Compose. Weโll spin up two containers:
A web application (Frontend).
A database (Backend).
๐ Step 1: Create the docker-compose.yml
File
This file will define both your containers (application and database). Here's a simple example:
version: '3.9'
services:
app:
image: nginx:latest
ports:
- "8080:80"
networks:
- app-network
depends_on:
- db
volumes:
- app-data:/usr/share/nginx/html
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: appdb
networks:
- app-network
volumes:
- db-data:/var/lib/mysql
networks:
app-network:
volumes:
app-data:
db-data:
๐ Step 2: Start the Containers
Use the following command to start the containers in detached mode:
docker-compose up -d
This command:
Spins up the web app and database containers.
Runs them in the background (detached mode).
Create a network for communication.
Attach volumes to store data persistently.
๐Step 3: Scale Your Application:
Want more replicas of your app? Easy! Run:
docker-compose up -d --scale app=3
๐ Step 4: Check the Status
Check the running containers:
docker-compose ps
Use this to verify if the containers are running.
๐ Step 5: View Logs
View the logs for a specific service:
docker-compose logs app
Check the logs of the app service to see if everythingโs working smoothly.
๐ Step 6: Bring Down the Containers
When youโre done, stop and clean up all resources:
docker-compose down
This stops and removes the containers, networks, and volumes.
๐๏ธ Task 2: Share Data Between Containers Using Docker Volumes
Goal:
Create two containers that share data using a named volume.
Time to see the magic of data sharing in action! ๐๏ธ
Letโs create two containers that share data through a Docker Volume.
๐๏ธStep 1: Create a Named Volume
Create a volume named shared_data
:
docker volume create shared_data
๐ Step 2: Create and Start Containers with a Shared Volume
Letโs create two containers that share the same data.
docker run -d --name container1 --mount source=shared-data,target=/data nginx
docker run -d --name container2 --mount source=shared-data,target=/data httpd
Here, we:
Created a volume named
shared-data
.Mounted it to the
/data
directory in both containers.
๐ Step 3: Verify Data Sharing
Create a file inside container1:
Add some data inside
container1
:docker exec container1 bash -c "echo 'Hello from Container 1' > /data/hello.txt"
Check the file in container2:
Check if
container2
can see the same datadocker exec container2 cat /data/hello.txt
You should see Hello from Container 1
, proving that both containers are sharing the same volume! ๐
๐ Step 4: List and Remove Volumes
List all volumes:
docker volume ls
Remove the containers and shared volume:
docker rm -f container1 container2
docker volume rm shared-data
โจKey Takeaways๐
Docker Volumes help you manage data that persists even when containers are removed.
Docker Networks enable communication between containers and isolate them for security.
Docker Compose, managing multi-container applications is a breezeโjust one file and a few commands to spin up, scale, and shut down your app.
๐ก Pro Tip: Start small and build up! Experiment with these commands and configurations, and soon you'll be deploying scalable apps like a pro. ๐งโ๐ป๐ฅ
Happy Docking! ๐ณ If you found this helpful, share your thoughts or questions below. Letโs grow together in our DevOps journey! ๐