Day 19 Docker Training: Managing Volumes and Networks
Table of contents
- 🗃️Docker Volumes
- 🌐Docker Networks
- ☑️Tasks:
- ✅Task1:Creating a Multi-Container Docker-Compose File
- 1️⃣Create a docker-compose.ymlfile
- 2️⃣Start the multi-container application:
- 3️⃣Check the status of the containers:
- 4️⃣View logs of a specific service:
- 5️⃣Stop and remove all containers, networks, and volumes:
- ✅Task 2: Sharing Data Between Containers Using Docker Volumes
- 1️⃣Creating Docker Volumes
- 2️⃣Using Docker Volumes
- 3️⃣Run command inside a running container
- 4️⃣Clone Application from Repository
- 5️⃣Create the Docker Image
- 6️⃣Create Network
- 7️⃣Run docker containers
- 8️⃣Enable the Port
- ➡️Output
- 💡Conclusion
🗃️Docker Volumes
Docker volumes allow Docker containers to persist and access data, making them essential.
Persistent Storage: Data stored in a volume exists independently of the container’s lifecycle. This means that when a container is removed, the data remains intact.
Data Sharing: Volumes can be attached to multiple containers, allowing them to seamlessly share data, which is especially useful for sharing configurations or databases among containers.
Creating Docker Volumes:
docker volume <valume_name>
🌐Docker Networks
Docker networks allow you to connect multiple containers, enabling them to communicate with each other and the host machine. Each container on a network can be accessed by other containers on the same network.
Creating Docker Networks:
docker network <network_name>
☑️Tasks:
✅Task1:Creating a Multi-Container Docker-Compose File
1️⃣Create a docker-compose.yml
file
version: '3'
services:
backend:
build:
context: .
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: myDb
depends_on:
- mysql
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myDb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
- mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage
volumes:
mysql-data:
2️⃣Start the multi-container application:
docker-compose up -d
3️⃣Check the status of the containers:
docker-compose ps
4️⃣View logs of a specific service:
docker-compose logs app
5️⃣Stop and remove all containers, networks, and volumes:
docker-compose down
✅Task 2: Sharing Data Between Containers Using Docker Volumes
1️⃣Creating Docker Volumes
docker volume create mysql-data
docker volume inspect mysql-data
2️⃣Using Docker Volumes
docker run -d --name mysql-demo -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7
3️⃣Run command inside a running container
ubuntu@ip-172-31-45-42:~$ docker exec -it mysql-demo bash
bash-4.2# mysql -u root -p
Enter password:
4️⃣Clone Application from Repository
5️⃣Create the Docker Image
docker build -t two-tier-app:latest .
6️⃣Create Network
docker network create twotier
7️⃣Run docker containers
docker run -d -v mysql-data:/var/lib/mysql --network twotier --name mysql -e MYSQL_DATABASE=devops -e MYSQL_ROOT_PASSWORD=root mysql:5.7
docker run -d --network twotier --name two-tier-app -p 5000:5000 -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DB=devops two-tier-app:latest
8️⃣Enable the Port
➡️Output
💡Conclusion
Docker Volumes and Networks are powerful tools for managing data persistence and container communication in our applications. The hands-on tasks provided give us practical experience in setting up and managing multi-container applications, making these skills a valuable addition to our DevOps toolkit.
Subscribe to my newsletter
Read articles from Ketan Hiray directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ketan Hiray
Ketan Hiray
💼 Senior Software Engineer 🚀 Enthusiast in DevOps Engineering