Docker Volume & Docker Network

Unnati GuptaUnnati Gupta
5 min read

#90daysdevopschallenge

#day19

Docker-Volume:

Docker-Volumes provides a feature to store the data of your container. It provides the facility to persist data outside the container, or database, in a particular location of your host machine.

By using docker-volume you never lose your data still you remove the container or delete the container.

Advantageous of Docker-Volume:

a) Data stored in volumes persist even if the container is stopped or removed.

b) Volumes can be shared with multiple containers, allowing them to access the same data.

c) Volumes provide better I/O performance compared to writing data directly into containers.

d) Volume in docker can be easily stored and backed up. You can manage volumes using Docker CLI and Docker API.

Docker provides some default volumes for specific purposes, such as /var/lib/docker/volumes for storing user-created volumes and /var/lib/docker/volumes/config for storing configuration data.

Docker Volume Command:

Creating a Docker Volume: To create volume in your system.

Syntax: docker volume create my_volume

List Volumes: list all the volumes on your system.

Syntax: docker volume ls

Inspect a volume: to get detailed information about a volume.

Syntax: docker volume inspect <volume_name>

Remove a volume: to remove a volume.

Syntax: docker volume rm <volume_name>

Use Volume in the container: To use volume inside the container you have to specify it when you run the container using the '-v' or '--volume' flag.

Syntax: docker run -d -v my_volume:/path/in/container my_image

Bind-Mounts:

It is a way to share a directory or file from the host machine with a docker container. It allows you to make a file or directory on the host machine directly accessible inside the container. Bind Mounts create a direct link between a directory or file on the host and a path inside the container. Syntax: -v /host/path:/container/path

Named Volume:

A name volume is created by using the docker volume create command.

Advantages of Named Volumes:

a) They are easier to manage and share between containers.

b) They are platform-independent, making them more portable.

Data Volume & Container Volume:

A data Volume is a volume that exists independently of any container and can be used by multiple containers but a container volume created inside the container only, can't be used with multiple containers. It creates with container and automatically removes it when the container is removed.

Docker Network:

Docker Network provides the facility to connect containers to each other or communicate with each other. It also provides the facility to connect with nondocker workloads.

A container has no information about what kind of network it's attached to, or whether its peers are also Docker workloads or not. A container only sees a network interface with an IP address, a gateway, a routing table, DNS services, and other networking details.

There are different types of networks in docker:

  1. Default bridge Network:

    When you install docker in your system, it by default creates and uses a bridge driver and when you create any container in your system, it creates a virtual ethernet network. for every container there is one virtual ethernet network, It acts like a switch.

  2. User-defined Network:

    In docker, you can create your own network, it is helpful in production to create your own network in place to use the default network and it also provides isolation to the default network.

  3. The HOST Network:

    In docker, if you don't want to expose the port, you can use the host network directly. In this case, you can directly access your application by using your host IP. but it has one disadvantage i.e. it has no isolation.

    It is accessed without using the port.

Network Drivers:

Network Drivers tell you which type of network are you using.

Network Drivers = Type of Network

How an IP address is assigned to a container:

By default, the container gets an IP address for every Docker Network it attaches to it. A container receives an IP address out of the IP subnet of the network. The docker daemon performs dynamic subnetting and IP address allocation for containers. You can connect multiple networks with one container.

Create a new docker network:

Syntax: docker network create <network_name>

List docker network: To list all networks in your docker system, use this command.

Syntax: docker network ls

Inspect Docker Network: To get detailed information about a specific docker network, including its configuration and connected containers.

Syntax: docker network inspect <network_name>

Connect a container to a network: To connect an existing container to a docker network.

Syntax: docker network connect <network_name> <container_name>

Disconnect a container from a network: To disconnect a container from a network.

Syntax: docker network disconnect <network_name> <container_name>

Remove a Docker network: To remove a Docker network.

Syntax: docker network rm <network_name>

โ„Tasks:

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 )

If you are facing any problem related to docker-compose, go through my previous blog:

https://devunnatig.hashnode.dev/docker-compose-for-devops-engineer

2.Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

3.Create two or more containers that read and write data to the same volume using the docker run --mount command. Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

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

In the Next Article, we will explore more in Docker......

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!

0
Subscribe to my newsletter

Read articles from Unnati Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Unnati Gupta
Unnati Gupta

๐Ÿ‘จโ€๐Ÿ’ป DevOps Engineer at 6D Technology Passionate about bridging the gap between development and operations, I'm a dedicated DevOps Engineer at 6D Technology. With a strong belief in the power of automation, continuous integration, and continuous delivery, I thrive in optimizing software development pipelines for efficiency and reliability. ๐Ÿš€ Exploring the DevOps Universe In my articles, I delve into the fascinating world of DevOps, where I share insights, best practices, and real-world experiences. From containerization and orchestration to CI/CD pipelines and infrastructure as code, I'm here to demystify the complex and empower fellow developers and ops enthusiasts. ๐Ÿ“ Blogging for Knowledge Sharing As a tech enthusiast and a lifelong learner, I'm committed to sharing knowledge. My articles aim to simplify complex concepts and provide practical tips that help teams and individuals streamline their software delivery processes. ๐ŸŒ Connect with Me Let's connect and explore the ever-evolving landscape of DevOps together. Feel free to reach out, comment, or share your thoughts on my articles. Together, we can foster a culture of collaboration and innovation in the DevOps community. ๐Ÿ”— Social Links LinkedIn: https://www.linkedin.com/in/unnati-gupta-%F0%9F%87%AE%F0%9F%87%B3-a62563183/ GitHub: https://github.com/DevUnnati ๐Ÿ“ฉ Contact Have questions or looking to collaborate? You can reach me at unnatigupta527@gmail.com Happy Learning!!