Docker Volume
In Docker, a volume is a managed data storage area that exists independently of a container's life cycle. It allows for persistent data storage, detached from a specific container, and can be shared among multiple containers. Volumes are managed by Docker and can be easily backed up, moved, and restored. They are designed to facilitate data sharing and persistence across containers within the Docker environment. Volumes do not expand the size of containers that use them. Instead, they act as separate storage entities outside the container's main structure. This means that even if a container is removed or replaced, the data within the volume remains intact and unaffected.
Using the -v
flag in Docker commands is concise but combines all options into a single field, making it less explicit. On the other hand, the --mount
flag provides a more explicit and verbose approach by separating the options. The key distinction lies in how the syntax is structured. Here's a comparison illustrating the syntax for each flag:
-v Syntax:
docker run -v /host/path:/container/path -v /data:/data:ro -v /more/data
--mount Syntax:
docker run --mount type=bind,source=/host/path,target=/container/path --mount type=bind,source=/data,target=/data,readonly --mount type=volume,source=/more/data
The --mount
flag separates the options explicitly, making it clearer and more organized, while the -v
syntax combines them into a single field, sacrificing some readability for brevity.
There are different types of volumes in Docker
Named Volume: These are named entities managed by Docker. They are more convenient and easier to manage as Docker takes care of the location and lifecycle.
docker volume create my_volume docker run -v my_volume:/path/in/container my image
Anonymous Volumes: These are created when a container needs a private volume that doesn't need to be shared or managed outside that container.
docker run -v /path/in/container my_image
Bind Mounts: These allow you to specify a directory on the host machine that will be mounted into the container.
docker run -v /host/path:/container/path my_image
Common Docker Volume Commands
Create a volume
docker volume create my_volume
List volumes
docker volume ls
Inspect a volume
docker volume inspect my_volume
Remove a volume
docker volume rm my_volume
Docker Network
In Docker, a network is a virtual communication bridge that allows containers to interact with each other and with other resources. It provides a means for containers to communicate within the Docker environment. Docker supports different types of networks, such as bridge, host, overlay, and macvlan, each serving specific connectivity purposes. Networks enable containers to share data and communicate in isolated environments, offering flexibility in managing how containers interact and exchange information.
Common Docker Network Commands
Create a Network
docker network create <network_name>
List Networks
docker network ls
Inspect a Network
docker network inspect <network_name>
Connect a Container to a network
docker network connect <network_name> <container_name>
Disconnect a Container from a network
docker network disconnect <network_name> <container_name>
Remove a Network
docker network rm <network_name>
Task 1
Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
version : "3.3"
services:
web:
image: varsha0108/local_django:latest
deploy:
replicas: 2
ports:
- "8001-8005:8001"
volumes:
- my_django_volume:/app
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
volumes:
my_django_volume:
external: true
The configuration implies that the web
service container requires the my_django_volume
volume to persist data within the /app
directory. This volume is assumed to exist beforehand or be created externally. If the my_django_volume
volume does not exist, you can create it using the following command.
docker volume create my_django_volume
- Use the
docker-compose up
command with the-d
flag to start a multi-container application in detached mode.
- Use the
docker-compose scale
command to increase or decrease the number of replicas for a specific service. You can also addreplicas
in the deployment file for auto-scaling.
- Use the
docker-compose ps
command to view the status of all containers, anddocker-compose logs
to view the logs of a specific service.
- Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.
Task 2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
- Create a new volume -
docker volume create ubuntu_volume
Create two or more containers that read and write data to the same volume using the docker run --mount
command.
docker run -d --name nginx_container_1 -v ubuntu_volume:/app nginx:latest docker run -d --name nginx_container_2 -v ubuntu_volume:/app nginx:latest
Verify that the data is the same in all containers by using the docker exec
command to run commands inside each container.
Use the docker volume ls
command to list all volumes and the docker volume rm
command to remove the volume when you're done.
Further insights on Docker volumes can be found on SpaceLift's blog. Explore more on this topic here.
Thank you!