📍Introduction
Docker has two options for the container to store files on the host machine so that the files are persisted even when the container stops. On the other hand, one of the reasons Docker containers and services are so powerful is that you can connect them, or connect them to non-Docker workloads.
🔹What is Docker Volume?
Docker Volume is a way to store data is used by Docker containers. It allows persist data outside the container and can be used to share data between containers. With Docker Volume you can mount a directory or a file from your host machine or a shared storage system into the container. This makes it easier to manage data between containers and allows data to persist even if the container is stopped or destroyed.
⚜How do Docker Volumes work?
When you create a Docker Volume you can specify the location where the volume should be created on the host machine or use a pre-existing directory. Once the volume is created, you can use docker run
command with the -v
or --mount
option to mount the volume into the container**.**
⚜Demonstration of Docker Volume
Create a volume using docker volume create
command:
List all the docker volumes using docker volume ls
command:
Inspect the Docker volume using docker volume inspect
command:
Mount the Docker volume to the container:
We can see the Docker volume is created inside/var/lib/docker/volume
🔹What is Docker Networking?
Docker Networking is a way to enable communication between Docker containers. When you create a Docker container, it is isolated from the host system and other containers by default. To enable communication between containers, you can create a Docker network. A Docker Network is a virtual network that enables communication between containers running on the same host or different hosts. Docker Network also provides isolation and security for your containers, making it easier to manage and secure your application.
⚜Network Driver
Network Drivers in Docker are a way to enable different types of networking in Docker containers. Docker provides several built-in network drivers that are optimized for different use cases, and you can also create your custom network drivers.
Here are some of the build-in network drivers in Docker:
Bridge network: This is the default network driver for docker. It creates a virtual network on the host machine and assigns a unique IP Address to each container. Containers on the same bridge network can communicate with each other using their IP addresses.
Host network: This driver removes the network isolation between containers and the host machine. This means that the container uses the same network as the host machine and can use the same IP address and port as the host.
Overlay network: This driver creates a virtual network that spans multiple Docker hosts. It enables communication between containers running on different hosts and provides a scalable and flexible network for distributed applications.
Macvlan network: This driver allows you to assign a MAC address to a container, which can be useful for scenarios where you need to simulate a physical network interface.
None network: This driver disables networking for a container, which can be useful for debugging or for containers that do not require network access.
Third-party network drivers: Docker also supports third-party network drivers, which can be used to implement custom networking solutions or to integrate with the existing networking infrastructure.
⚜What is Port Mapping in Docker?
Port mapping is a way to expose ports from a Docker container to the host machine or other containers. It enables the communication between containers and between containers and the outside world.
To map ports in Docker, you can use the -p
or --publish
option when running a container. The syntax is as follows:
docker run -p <host-port>:<container-port> <image>
For example, if you want to run a container based on the nginx
image and expose port 80 to the host machine on port 8080, you can use the following command:
docker run -p 8080:80 nginx
In this command, -p 8080:80
maps port 80 in the container to port 8080 on the host machine.
You can also specify multiple port mappings by using multiple -p
options. For example:
docker run -p 8080:80 -p 8443:443 nginx
In this command, port 80 in the container is mapped to port 8080 on the host machine, and port 443 in the container is mapped to port 8443 on the host machine.
📍Conclusion
Docker volume is used to store and persist data used by Docker containers while Docker Network is used to enable communication between Docker containers. Both are essential features of Docker that make it easier to manage and run containerized applications. Docker Volume provides a simple and flexible way to manage data used by Docker containers. Port Mapping is a useful feature in Docker that enables the communication between containers and the outside world.