-
Notifications
You must be signed in to change notification settings - Fork 0
Docker: Docker Networks
Docker network enables communication between Docker containers, allowing them to securely exchange data and interact with each other within isolated network environments.
Docker Networks what are they? Very-Simple, imagine them as a bridge between containers and the external environment. It allows traffic to travel between the containers (internal environment) to external systems (external environment). This is done by offering various networking models and configurations, Docker Networks empower developers to design flexible, scalable and secure network architecture for their containerized applications. It helps with restricting access to some container services.
Docker Networks offer versatile solutions for containerized environments, allowing seamless communication and connectivity. Let's delve into some prominent use cases where Docker Networks play a crucial role:
-
Microservices Architecture: Docker Networks enable communication between microservices in containerized applications, supporting scalability and fault isolation.
-
Multi-Tier Applications: Docker Networks facilitate the deployment of multi-tier applications by connecting frontend, backend, and database containers, ensuring secure and efficient communication.
-
Hybrid Cloud Deployments: Docker Networks allow for seamless communication between containers across on-premises and cloud environments, enabling organizations to leverage both infrastructures effectively.
Docker Networks provide indispensable capabilities for a variety of use cases, including microservices architecture, multi-tier applications, and hybrid cloud deployments. By enabling efficient communication and connectivity between containers.
If you ever created a docker network over Portainer you may have noticed that there are multiple network types you could use to configure your own customized docker network. Then you also may have realized that they aren't really types but more akin to divers for the docker network. There a five different drivers, these drivers are the following:
bridge
host
overlay
ipvlan
macvlan
none
The effects on the docker network depend on the driver used, each driver has a different effect. See below for more information:
-
bridge:
When using thebridge
driver the docker network will use the default network driver, meaning it'll have access to the internet of the hosts driver. It uses NAT for this, to create a distinct network from the host. -
host:
When using thehost
driver the docker network will remove the isolation between host and container network, meaning it will directly use the network interface. Meaning that they wont have their own dedicated network stack and ip-address'. -
overlay:
When using theoverlay
driver for the docker network the containers within the docker networks are most likely operating on docker swarm, cloud or different hosts/nodes. Theoverlay
driver allows to have interconnectivity between multiple nodes, by allowing the daemons to communicate with each other. -
ipvlan:
When using theipvlan
driver in Docker, the Docker network leverages the physical or virtual interfaces of the host directly. This unique approach offers enhanced performance compared to other network drivers, as it circumvents the overhead associated with virtual bridges. Additionally, it grants administrators full control over IPv4 and IPv6 configurations. -
macvlan:
When using themacvlan
driver in Docker, containers are indeed assigned MAC addresses that are distinct from the host's MAC address. These MAC addresses are typically associated with the containers' virtual network interfaces, which are created by the macvlan driver. -
none:
When using thenone
driver or simply said no driver it will completely isolate the docker containers contained in the docker network from the outside world (Outside world being external environment e.g. Host and other docker networks and nodes)
Creating a basic docker network is pretty easy. As you now know docker networks are here in order to isolate containers from the host system (logically speaking).
According to this logical network plan we have one docker network with one phpmyadmin
and mysql
container in it. replicating this is pretty easy for this we only need 7 commands which can be shortened to 5, since you can leave out the docker image pull
command, because it does this automatically if the image isn't available on local.
These 7 Commands will do the following:
- Creation of docker network
- image pull of phpmyadmin
- image pull of mysql
- creation of docker volume for phpmyadmin
- creation of docker volume for mysql
- creation of docker container mysql
- creation of docker container phpmyadmin
Okay, first we start with creating the docker network fortunately for us it is easy since the command is structured simple:
docker network [Option] [Value]
In our case option
would be create
and our value
would be network.test
. The command that does this would look like this:
docker network create network.test
With this we have successfully created the docker network network.test
Pulling a image, off of a docker contaier repository is very simple in nature just copy and paste the following command:
docker pull [image:tag]
In our case [image:tag]
will be replaced with the actual name and tag of our docker image we want to use:
docker pull phpmyadmin:latest
Now you have a docker image on your computer you can verify this by executing the following command:
docker image ls
this will output the following
REPOSITORY TAG IMAGE ID CREATED SIZE
phpmyadmin latest 5f11582196a4 16 months ago 287MB
Similarly, let's pull the MySQL image using the following command:
docker pull mysql:latest
This will download the latest MySQL image from the Docker repository onto your local machine. To confirm that the image has been successfully downloaded, you can execute:
docker image ls
This command will display a list of Docker images currently available on your machine, including the newly pulled MySQL image.
REPOSITORY TAG IMAGE ID CREATED SIZE
phpmyadmin latest 5f11582196a4 16 months ago 287MB
mysql latest 5f11821245a4 16 months ago 136MB
Now, let's create a Docker volume for phpMyAdmin using the following command:
docker volume create phpmyadmin_data
This will create a Docker volume named phpmyadmin_data
which will be used to persist phpMyAdmin data.
Similarly, create a Docker volume for MySQL with the following command:
docker volume create mysql_data
This command will create a Docker volume named mysql_data
which will be used to persist MySQL data.
Now, let's create a Docker container for MySQL using the following command:
docker run -d --name mysql_container --network network.test -v mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root_password mysql:latest
This command will create a Docker container named mysql_container
running MySQL, connected to the network.test
network, and utilizing the mysql_data
volume to persist its data. Replace root_password
with your desired MySQL root password.
Finally, create a Docker container for phpMyAdmin using the following command:
docker run -d --name phpmyadmin_container --network network.test -p 8080:80 -v phpmyadmin_data:/var/lib/phpmyadmin -e PMA_HOST=mysql_container phpmyadmin:latest
This command will create a Docker container named phpmyadmin_container
running phpMyAdmin, connected to the network.test
network, and accessible via port 8080 on the host machine. It will utilize the phpmyadmin_data
volume to persist its data and will be configured to connect to the MySQL container named mysql_container
.
a good resource for this would be the following: