Docker-Volume, Docker Network: #Day19 of 90DaysofDevOps
Table of contents
Docker-Volume
Docker allows you to create a separate storage area called Volumes. Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.
They allow you to store data, like a database, outside the container, so it doesn't get removed when the container is deleted.
Docker Network
Docker Networking is to connect the docker container to each other and the outside world so they can communicate with each other and also they can talk to Docker Host.
You can connect docker containers to non-Docker workloads. Docker uses CNM Container Network Model for networking. This model standardizes the steps required to provide networking for containers using multiple network drivers.
Docker offers a mature networking model. There are three common Docker network types โ
bridge networks: used within a single host,
overlay networks: for multi-host communication,
macvlan networks: which are used to connect Docker containers directly to host network interfaces.
Why Docker Network?
The Docker network is a virtual network created by Docker to enable communication between Docker containers. If two containers are running on the same host they can communicate with each other without the need for ports to be exposed to the host machine
Task-1
Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
Use the
docker-compose up
command with the-d
flag to start a multi-container application in detached mode.docker-compose up -d
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.docker-compose scale
Use the
docker-compose ps
command to view the status of all containers, anddocker-compose logs
to view the logs of a specific service.docker-compose ps
docker-compose logs
Use the
docker-compose down
command to stop and remove all containers, networks, and volumes associated with the applicationdocker-compose down
Task-2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
Use the below command to create a volume.
docker volume create --name django-todo-volume --opt type=none --opt device=/home/ec2-user/nodejs-project-day17/django-todo/volume/docker-todo-volume --opt o=bind
-
Create two or more containers that read and write data to the same volume using the
docker run --mount
command.docker run -d --mount source=django-todo-volume,target=/data -p 80:80 django-app:latest
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
docker exec -it <contained-id> bash
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
docker stop [contianer-id]
docker rm [container-id]docker volume rm [volume name]
Thank you for reading. Happy Learning ๐!!!