# Day 19 Task: Docker for DevOps Engineers part-3

#Docker-Volume

\-Docker volumes are **file systems mounted on Docker containers to preserve data generated by the running container**.

\-The data doesn't persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.

\-Volumes are the preferred mechanism for **persisting data generated by and used by Docker containers**. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

#Docker- Networking

\-Docker networking **allows you to attach a container to as many networks as you like**.

\-You can also attach an already-running container. Go ahead and attach your running web app to my\_bridge.

\-There are three common Docker network types – **bridge networks, used within a single host, overlay networks, for multi-host communication, and macvlan networks** which are used to connect Docker containers directly to host network interfaces.

\-Container networking **allows containers to communicate with other containers or hosts to share resources and data**. Modern container networking aims to standardize and optimize container data flows, creating zones of isolation that allow large numbers of containers to communicate efficiently and securely.

<table><tbody><tr><td colspan="1" rowspan="1"><p>#task1</p></td></tr><tr><td colspan="1" rowspan="1"><p>i). Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot</p></td></tr><tr><td colspan="1" rowspan="1"><p>(Example - Create application and database container)</p></td></tr></tbody></table>

* Use the `docker-compose up` command with the `-d` flag to start a multi-container application in detached mode.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680675857998/72a81ef4-40f3-4501-ae1e-7770fa8a94f3.png align="center")

* Use the `docker-compose scale` command to increase or decrease the number of replicas for a specific service. You can
    
* also add [`replicas`](https://stackoverflow.com/questions/63408708/how-to-scale-from-within-docker-compose-file) in the deployment file for *auto-scaling*.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680676487765/edd75c1f-baea-449d-8486-6aadd156292c.png align="center")

* Use the `docker-compose down` command to stop and remove all containers, networks, and volumes associated with the application.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680676469104/f49ea69e-12fb-4189-9b68-7a72340dd659.png align="center")

<table><tbody><tr><td colspan="1" rowspan="1"><p>#Task-2</p></td><td colspan="1" rowspan="1"><p></p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p></p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p>i). Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.</p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p></p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p>ii). Create two or more containers that read and write data to the same volume using the docker run --mount command.</p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p></p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p>iii). Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.</p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p></p></td></tr><tr><td colspan="1" rowspan="1"><p></p></td><td colspan="1" rowspan="1"><p>iv). Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.</p></td></tr></tbody></table>

i). Create a Docker volume:

sudo docker volume create my\_volume

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680676620695/3019f785-169f-4509-b0b6-ff4e6958250e.png align="center")

ii). Volume inspect

docker volume inspect mt\_volume

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680688204394/c006f025-a6e1-480c-9915-f0e33b8bcd0b.png align="center")

iii)Create two or more containers that read and write data to the same volume using the docker run --mount command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680688715304/aa470ec4-bdeb-4732-95a5-0c190e772a1f.png align="center")

iv). Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

verification

% docker exec -it 7708dbcd7b22 sh

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680689433159/b3f4cf82-3d0a-4189-aa10-1eb4e055aa7f.png align="center")

v). Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.

\-docker volume ls

\-docker stop 7708dbc7b22

\-docker volume rm 7708dbc7b22

\-docker volume rm my\_volume

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680689667812/641ac2e2-0397-41c7-b89e-b13acca49660.png align="center")
