Docker Volumes and Networking

Docker Volumes and Networking

·

3 min read

Docker has revolutionized the way we develop, ship, and deploy applications. Two crucial aspects of Docker that facilitate this seamless application management are Docker volumes and networking. In this blog, we will explore Docker volumes and networking, and how they work in our containerized applications.

Why do we need Docker Volumes?

When a Docker container is stopped or removed, all changes made inside the container's filesystem are lost. This problem can be solved with Docker volumes which provide a way to persist data beyond the lifecycle of a container.

Docker volumes allow common data to be shared among multiple containers which is helpful for microservices architectures.

Docker volumes are a critical feature in Docker that allows you to manage and persistently store data used by Docker containers. They are essential for the following reasons:

  1. Data persistent

  2. Data Sharing

  3. Recovery and backup

  4. Isolation

Docker Volumes Basic Commands:

#Listing docker volumes
docker volume ls
#Creating a docker volumes
docker volume create myvol
#Inspecting a docker volume
docker volume inspect myvol
#Mounting a docker in a container
docker run -d -v myvol:/app/data <image_name>
#Removing a volume
docker volume rm myvol

Docker provides two primary methods for managing data between the host and containers: volumes and bind mounts. The key differences between volumes and bind mounts:

Aspect

Volumes

Bind Mounts

Data Location

Managed by Docker, separate from host filesystem

Directly from host filesystem into container

Data Persistence

Persistent (survives container removal)

Not inherently persistent (depends on host)

Portability

Portable, platform-independent & not tied to host paths

Less portable, relies on host-specific file paths

Backup and Restore

Easy to back up and restore data

Relies on host backup methods

Location on Host

Stored in '/var/lib/docker/volumes/'

Directly mapped to host filesystem

Sharing Data Between Host and Containers

Does not expose host files directly

Exposes host files directly to the container

Docker Networking

Docker networking is another essential aspect of containerization. It allows containers to communicate with each other and the outside world.

Docker provides various networking options, each suited to specific use cases.

  1. Default Network Bridge: When we install Docker, it creates a default network bridge called Bridge. Containers that are attached to this network can communicate with each other, but they are isolated from the host and external networks by default.

  2. Custom Bridge Networks: Custom bridge networks offer greater control over container communication and can be connected to external networks or kept in isolation.

  3. Host Networking: Containers can use the host network directly, bypassing Docker's network stack. This can be useful for applications that require direct access to the host's network interfaces.

  4. Overlay Networking: These are suitable for multi-host deployments, such as Docker Swarm or Kubernetes clusters. Overlay networks allow containers to communicate seamlessly across different hosts.

Docker Network Basic Commands:

#Listing networks
docker network ls
#Inspecting docker network
docker network inspect <network_name>
#Creating custom bridge network
docker network create mynetwork
#Connecting network to a container
docker network connect mynetwork <container_name/id>
#Assigning network to a container while running container
docker run -d --network=mynetwork <image name>
#Disconnecting network from a container
docker network disconnect mynetwork <container name/id>
#Removing docker network
docker network rm mynetwork
#Creating an overlay network
docker network create --driver overlay my_overlay_network

Docker volumes and networking are essential components of containerized applications which play a crucial role in ensuring our containers run smoothly and reliably. By understanding the various types of volumes, and networking options we can harness the full power of Docker.