The comprehensive guide to Docker Networking and Volumes will show you everything you need to know about how network and volume work in Docker. Getting started …
Have you ever wondered how docker connects an application from host to container and docker networking works in the Linux system? Docker takes care of the networking part so that containers can communicate within themselves also with the docker host.
There are different network drivers used in docker in order to connect with one another.
Bridge: This is the default networking driver used by the docker. This driver creates a bridge between the docker host and the container in order to communicate with each other.
docker run -d -p 8080:80 nginx:latest
The command which had shown below is to run an Nginx container using the Nginx image from the docker hub. The above example uses the docker bridge network driver by default which sends traffic from port 8080 in the docker host to the container port 80.
If you want to list all the docker networks use the docker network ls command.
After running the Nginx container if you inspect the container using the docker inspect <contianer_name> command you can see the network driver attached to it
As in the screenshot, you can see the bridge network driver is attached to the container which sends traffic from docker host port 8080 to container port 80.
If you want to inspect the bridge network use the below command. It will provide the details of all containers attached to this network.
docker network inspect bridge
Host: If you are using the host as a network bridge for your container that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP address allocated.
For example, if you are running Nginx as a container it used the default port 80 of the host and the application will be visible in the host IP address. If you want to run an Nginx container using the host network use the below command.
Using this network might cause ports to conflict because if we want to run 2 webserver container’s it uses the same default port 80 on the host which is not applicable.
Here the container will use the host port 80 in order to send traffic outside by default.
docker run –rm -d –network host –name nginxcontianer nginx:latest
There are also other networks like overlay,ipvlan,macvlan etc which are more advanced topics and will cover in future blogs.
You can create your own network and add containers to it so that one container can communicate with another using the same network.
Using this command you can create your own network
docker network create –-driver drivername name
Driver’s can be either bridge, host,overall,macvlan etc
You can get more details about docker networking from the official documentation https://docs.docker.com/network/.
Volumes are the mechanism used for preserving data used by the docker containers. So the data will not be lost even if we remove the container. When we are talking about docker volumes there are 2 types of docker volumes,
With Bind Mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full or relative path on the host machine. To use bind mounts, the file or directory does not need to exist on your Docker host already. If it doesn’t exist, it will be created on demand. Bind mounts rely on the host machine’s filesystem having a specific directory structure available. You must explicitly create a path to the file or folder to place the storage.
In order to create a bind mount use the below command.
docker run –rm –name postgres-db -e POSTGRES_PASSWORD=password -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data -p 2000:5432 -d postgres
This command is running a Postgres container using the image from the docker hub here the bind mount is specified by the flag -v.
-V indicates that the directory /$HOME/docker/volumes/postgres in the host is bound with the data of /var/lib/postgres/data which is the data directory of Postgres. After running the command if we list down the contents inside /$HOME/docker/volumes/Postgres directory we can Postgres data directory.
With Volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s content. Docker volumes are completely handled by Docker itself and therefore independent of both your directory structure and the OS of the host machine.
Where are docker volumes stored?
When you create a docker volume, a new directory is created within Docker’s storage directory on the host machine in Linux it will be in the /var/lib/docker/volumes/volume_name/_data directory, and Docker manages that directory’s contents.
You can create a named volume using the below command here, I am creating a volume named myvol
docker volume create myvol
After creating the volume if you run the same container again with the volume you specified as shown below it will be mounted to the directory /var/lib/docker/volumes.
docker run –rm –name postgres-db -e POSTGRES_PASSWORD=password -v myvol:/var/lib/postgresql/data -p 2000:5432 -d postgres
From the above explanation, we understood that the docker bind volume is used to bind a container directory or file to any location inside the docker host But the named volumes are volumes which are handled by the docker itself so the contents will be mounted to the docker default volume directory /var/lib/docker/volumes.
In order to remove the docker volumes use the command
docker volume rm volume_name
Note that you cannot remove the volume attached to a running container using the above command inorder to do it you need to pass the -f (force )flag within the command
Summary: In this blog, we have learned about docker networking and storage.