Understanding Docker Volumes and Docker Networks
Simple explanation and practical guide on how to create and use Docker Volumes and Networks.
π³ Docker Volumes and Docker Networks
This guide explains two important concepts in Docker β Volumes (for data storage) and Networks (for container communication). Both are essential for managing real-world applications where containers need to store data and talk to each other.
πΎ What is a Docker Volume
A Docker Volume is a storage mechanism used to persist data generated or used by containers. Normally, data inside a container is deleted when the container is removed, but with volumes, data stays safe even after deletion.
In short:
- Volumes are used to store and share data between containers.
- They are managed by Docker and stored outside the containerβs filesystem.
βοΈ Step 1: Create a Docker Volume
You can create a volume manually using:
docker volume create myvolumeThis command creates a volume named myvolume that can be used by any container.
π§© Step 2: Use Volume in a Container
To attach a volume to a container:
docker run -d \
--name mycontainer \
-v myvolume:/usr/share/nginx/html \
nginxExplanation:
-v myvolume:/usr/share/nginx/htmlβ mounts themyvolumeto the given path inside the container.- Any data written to
/usr/share/nginx/htmlinside the container will be stored persistently inmyvolume.
π§Ύ Step 3: Check and Inspect Volumes
To see all volumes:
docker volume lsTo inspect details about a specific volume:
docker volume inspect myvolumeπ§Ή Step 4: Remove Volumes
Remove an unused volume:
docker volume rm myvolumeRemove all unused volumes:
docker volume pruneπ‘ Tip: Never use container storage for databases or important files β always use volumes for persistence.
π What is a Docker Network
A Docker Network allows containers to communicate with each other securely and efficiently. By default, containers are isolated β Docker networks connect them together.
In short:
- Containers on the same network can talk to each other using container names.
- You can create multiple custom networks for different applications.
βοΈ Step 1: Create a Docker Network
To create a new network:
docker network create mynetworkThis creates a bridge-type network by default, which allows containers to communicate with each other on the same host.
π§© Step 2: Run Containers on the Same Network
Run two containers and connect them to the same network:
docker run -d --name app1 --network mynetwork nginx
docker run -d --name app2 --network mynetwork httpdExplanation:
- Both
app1andapp2are now connected tomynetwork. - They can communicate using container names, e.g.,
ping app2from insideapp1.
π§Ύ Step 3: Inspect Network Details
To see available networks:
docker network lsTo see detailed info about a network:
docker network inspect mynetworkπ§Ή Step 4: Remove a Network
To remove a specific network:
docker network rm mynetworkRemove all unused networks:
docker network pruneUsing MySQL and MongoDB in Docker
Step-by-step guide on how to use MySQL and MongoDB with Docker β from pulling images to creating and running containers.
Docker Basics - Images, Containers, Networks, Volumes, and Compose
Concise notes explaining Docker, Images, Containers, Networks, Volumes, and Docker Compose.