Docker Storage and Network
Docker manages and handles storage-related aspects in the context of containers and containerized applications. Docker provides several mechanisms and options for managing storage to ensure data persistence, efficient resource utilization, and data management within containers.
Docker in terms of network enables communication and networking between Docker containers and with the external world. It allows containers to interact with each other and with external resources, such as other containers, the host system, or the internet. Docker provides various types of networks and network configurations to meet different use cases and security requirements.
In this article you will learn about how to setup your storage so you can overcome problems of crashing a container and others.
Storage
Docker uses storage drivers to manage the underlying storage infrastructure for containers. These drivers are responsible for handling read and write operations for the container’s filesystem. Docker supports various storage drivers, with the most common ones being OverlayFS, aufs, and device mapper. The choice of storage driver depends on the underlying storage infrastructure and the Docker installation.
Each Docker container has its own filesystem, which is based on a read-only image layer (the base image) and a read-write container layer. When a container is started, a new read-write layer is created, allowing the container to write data without affecting the underlying image. This separation ensures that the base image remains intact and can be shared among multiple containers.
Docker Volumes are a way to persist and manage data separately from the container’s filesystem. Volumes are directories or files that are stored outside the container and can be mounted into one or more containers. This is useful for sharing data between containers, backing up data, and ensuring data persistence even when a container is removed.
Per example:
docker run -v /opt/data:/var/lib/mysql -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 mysql
Here you are saving that that is on /var/lib/mysql into the host /opt/data. Even if there’s a crash you can re-run and all data will be there. This is also known as binding volumes, similar to volumes, bind mounts allow you to mount a directory from the host system into a container. Unlike volumes, bind mounts don’t have Docker management and are useful for sharing data or configuration files between the host and the container.
Networks
In terms of networks there are three main networks that are generated when a container is launched, those are: bridge, none and host.
To specify a specific network to use you can run:
docker run <image name> --network=<name of the network>
To know more info about it you can run the following command:
docker inspect <name of the network>
When you don’t specify the network you want Docker will use the default network which is default bridge network. Containers attached to this network can communicate with each other using their container names or IP addresses. However, by default, containers on this network are isolated from the host system.
To check all the existing networks in a container you can run:
docker network ls
Also you can create more networks on docker under specific subnet and gateway, here is an example:
docker network create --driver bridge --subnet 182.18.0.4/24 --gateway 182.18.0.1 <name of the network>
Conclusion
Now you know how to manage better your storage and also setup and manage the networks inside of your containers.