Docker Volume & Docker Network

Docker Volume & Docker Network

#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:

devunnatig.hashnode.dev/docker-compose-for-..

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 !!!