After exploring Docker Compose, today we delve into another essential aspect of Docker for DevOps engineers: Volumes and Networking. These features are critical for managing data persistence and enabling communication between containers.
🔹 What are Docker Volumes?
Docker volumes are a way to persist data generated by and used by Docker containers. They allow you to:
Store data outside the container: Data remains even when the container is deleted.
Share data between containers: Multiple containers can read and write to the same volume.
Separate container lifecycle from data lifecycle: Ensure your important data isn’t lost.
Key Commands for Docker Volumes:
List all volumes:
docker volume ls
Create a volume:
docker volume create my_volume
Remove a volume:
docker volume rm my_volume
🔹 What is Docker Networking?
Docker networks allow containers to communicate with each other and the host system. By default, Docker provides several network modes:
Bridge: Default network mode for containers.
Host: Containers share the host’s network.
Overlay: Used for multi-host communication in Docker Swarm.
None: No network is attached.
Key Commands for Docker Networking:
List networks:
docker network ls
Inspect a network:
docker network inspect <network_name>
Create a network:
docker network create my_network
Remove a network:
docker network rm my_network
🔹 Task 1: Multi-Container Setup with Docker Compose
For today’s first task, we’ll create a multi-container application using Docker Compose. This will involve an application container and a database container.
Steps:
Create a
docker-compose.yml
file to define the application and database services.Use the
docker-compose up -d
command to start all containers in detached mode.Scale the application service using the
docker-compose scale
command.View the status of the containers using
docker-compose ps
and logs usingdocker-compose logs
.Tear down the setup with
docker-compose down
.
Sample docker-compose.yml
:
version: "3.8"
services:
app:
image: nginx:latest
ports:
- "8080:80"
networks:
- app_network
database:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- app_network
networks:
app_network:
🔹 Task 2: Sharing Data with Docker Volumes
For the second task, we’ll learn how to use Docker Volumes to share data between multiple containers.
Steps:
Create a volume using
docker volume create shared_volume
.Run two containers with the same volume mounted using the
docker run --mount
command.Verify that changes made in one container are reflected in the other by accessing the volume.
Use
docker volume ls
to list all volumes anddocker volume rm shared_volume
to remove the volume after completing the task.
Example Commands:
# Create a volume
docker volume create shared_volume
# Run the first container with the volume
docker run -dit --name container1 --mount source=shared_volume,target=/data ubuntu
# Run the second container with the same volume
docker run -dit --name container2 --mount source=shared_volume,target=/data ubuntu
# Verify data sharing
docker exec -it container1 bash -c "echo 'Hello from Container 1' > /data/file.txt"
docker exec -it container2 cat /data/file.txt
# Cleanup
docker volume rm shared_volume
🔹 Key Learnings from Day 19
Docker volumes ensure data persistence and sharing across containers.
Docker networks facilitate container-to-container and container-to-host communication.
Multi-container setups with Docker Compose make managing applications easier.
Conclusion
Day 19 of the #90DaysOfDevOps challenge introduced me to the power of Docker volumes and networking. These features are essential for building scalable and reliable applications, making Docker an indispensable tool for DevOps engineers.
For more updates, follow me here: 🔗 Hashnode: https://hashnode.com/@patilsiddharth0411
🔗 Medium: https://medium.com/@patilsiddharth0411
🔗 LinkedIn: www.linkedin.com/in/siddharthpatil112000
Special thanks to Train with Shubham for guiding me on this journey!
#90DaysOfDevOps #Docker #DockerVolumes #DockerNetworking #DevOps #CloudComputing #LearningTogether #TechCommunity
DevOps | AWS | Linux | 90DaysOfDevOps | Docker