Docker Volumes and Networking in Containerization

Ajay GiteAjay Gite
7 min read

Docker-Volume

Docker volumes are a way to manage and persist data outside of a container. In the world of containers, where containers can be created and destroyed, having a way to store and share data beyond the container's lifecycle is important. Volumes allow you to store data separately from the container filesystem, ensuring that the data persists even if the container is removed.

Creating a Volume:

sudo docker volume create <volume-name>

List the docker volume:

sudo docker volume ls

Check if the docker volume exists:

sudo docker inspect <volume-name>

Remove volume:

sudo docker volume rm <volume-name>

Removing all volumes:

sudo docker volume prune

There are two ways to link a container to a volume:

— mount flag-v or —volume flag

[-- mount] flag consists of:

type=volume, source=<name_of_volume>, target=<target_path>

sudo docker run -d —name Test-1 —mount source=new-vol,target=/app nginx:latest

[ - - volume or -v] flag consists of three fields separated by colons

<name_of_the_volume> : <destination_path> : <optional_config>

sudo docker run -d —name Test-2 —volume new-vol-2:/app nginx:latest

Volume Types:

Tmpfs mount - used to temporarily store sensitive data in memory

Named or anonymous volume - managed by Docker using its CLI

Bind mount - arbitrary directory mounted from host to container

Docker Network

Think of Docker networks as virtual highways connecting different containers within your Docker environment. Just like real-life highways connect various cities, Docker networks connect different containers, enabling them to communicate and share information with each other.

Example:

Imagine you have a microservices-based application where each service is a separate Docker container. These services need to communicate with each other to perform tasks. Docker networks help these containers talk to one another, just like how different teams in an office need to collaborate to get work done.

Here are examples of different types of Docker networks:

1. Bridge Network (Default): Think of a bridge network as a virtual office floor where different teams have their own designated space. Each team can work independently, but they can also collaborate when needed. Similarly, containers in a bridge network can communicate with each other, and they are isolated from containers in other networks.

Example: You have multiple applications like HR, Sales, and Marketing. You create a bridge network for each department. Containers within the same network can easily interact, but they are separate from containers in other networks.

docker run -d --network=bridge -p 80:80 nginx

2. Host Network: Imagine a co-working space where different companies share a common area. Here, containers are directly connected to the host's network, just like companies connecting to the shared space's resources.

Example: You have containers that need direct access to your host machine's network, like monitoring tools. By using the host network, these containers can access resources without additional network setup.

docker run -d --network=host nginx

3. Overlay Network: Picture a company with multiple branch offices located in different cities. Each office operates independently, but they need to communicate seamlessly. Overlay networks connect containers across different Docker hosts, just like branches of a company staying connected.

Example: You're running containers on different servers across multiple locations. Overlay networks enable containers to communicate across servers as if they're on the same network.

docker network create -d overlay my_overlay_network
docker service create --network=my_overlay_network --name=web nginx

4. Macvlan Network: Think of a macvlan network as assigning a dedicated phone number to each employee in an office. Similarly, each container in a macvlan network gets a unique MAC address and can be assigned an IP address like an individual entity.

Example: You want each container to have its own IP address as if they were separate physical machines. Macvlan networks are useful when you need containers to appear as separate devices on the network.

docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 my_macvlan_network
docker run -d --network=my_macvlan_network --name=container1 nginx
  • docker network create: This is the command for creating a Docker network.

  • -d macvlan: This specifies that you want to create a macvlan network.

  • --subnet=192.168.1.0/24: This sets the subnet for the macvlan network to 192.168.1.0/24. This subnet defines the IP address range that containers on this network can use.

  • --gateway=192.168.1.1: This specifies the gateway IP address for the macvlan network. The gateway is used for routing traffic to and from the containers on the network.

  • -o parent=eth0: This option specifies the physical network interface (eth0 in this case) on the host to which the macvlan network is attached. Containers connected to this network will use eth0 as their parent interface on the host.

  • my_macvlan_network: This is the name you've given to the macvlan network you're creating. You can replace it with your desired network name.

5. None Network: Imagine you have a team working remotely from different locations. The "none" network is like isolating each team member in their own individual workspace with no direct communication between them.

Example: You want to run a container in complete isolation from any network. This can be useful for certain security-sensitive applications that shouldn't have any network access.

docker run -d --network=none nginx

6. Custom Bridge Network: Think of a custom bridge network as creating your own unique collaboration space within a co-working building. You define the rules for communication and interaction, allowing certain members to connect while keeping others separate.

Example: You need fine-grained control over communication between containers. You create a custom bridge network, add specific containers to it, and configure the network rules according to your requirements.

docker network create my_bridge_network
docker run -d --network=my_bridge_network --name=container1 nginx
docker run -d --network=my_bridge_network --name=container2 nginx

7. IPvlan Network: Imagine you have a large apartment building with multiple units, and each unit has its own separate entrance and address. The "ipvlan" network is similar to creating individual addresses for each unit within the same physical building.

In Docker, the ipvlan network type allows you to create multiple virtual interfaces, each with its own unique IP address, all associated with the same physical network interface. This enables containers to have their own distinct network identities, even though they share the same underlying network infrastructure.

Example: Consider a scenario where you have a server with a single physical network interface. You want to run multiple containers on this server, each with its own IP address and network isolation, but all using the same physical network connection.

docker network create -d ipvlan --subnet=192.168.0.0/24 --gateway=192.168.0.1 -o parent=eth0 my_ipvlan_network
docker run -d --network=my_ipvlan_network --name=container1 nginx

Refer: https://www.youtube.com/live/UNew_BBNVPk?feature=share

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 )

Checkout my repository using the following link: https://github.com/AjayGite/todo-flask-app

Task-2

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

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

    Create a Named Volume: Create a named volume using the docker volume create command. For example:

        docker volume create named-volume
    
  • Create Containers with Shared Volume: Create two or more containers that share the same named volume using the docker run --mount command. Here's an example:

        docker run -d --name container1 --mount source=named-volume,target=/app image_name
        docker run -d --name container2 --mount source=named-volume,target=/app images_name
    
  • Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

    Verify Shared Data: You can use the docker exec command to run commands inside each container to verify that they are using the same volume. For instance:

        docker exec -it container1 sh
        ls /app
    

    Write and Read Data: Inside each container, you can write and read data to/from the shared volume:

        touch file.txt
    

    Check Data in Other Containers: After writing data in one container, check if the data is visible in the other container:

      docker exec -it container2 sh
      touch /app/data.txt
      cat /app/data.txt
    
  • Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.

    List and Remove Volumes: You can list all volumes using the docker volume ls command:

        docker volume ls
    

    Remove Volume: When you're done, you can remove the named volume using the docker volume rm command:

        docker volume rm mydata
    

    This task demonstrates how Docker Volumes and Named Volumes can be used to share files and directories between multiple containers. The named volume mydata is created, and two containers (container1 and container2) are connected to it. Data written in one container is visible in the other due to the shared volume. Finally, the volume is removed when you no longer need it.

1
Subscribe to my newsletter

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

Written by

Ajay Gite
Ajay Gite