Skip to content

Docker: Docker Networks

Rayan edited this page Mar 20, 2024 · 16 revisions

Docker Networks

Docker network enables communication between Docker containers, allowing them to securely exchange data and interact with each other within isolated network environments.

Definition

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.

Use Cases

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:

  1. Microservices Architecture: Docker Networks enable communication between microservices in containerized applications, supporting scalability and fault isolation.

  2. Multi-Tier Applications: Docker Networks facilitate the deployment of multi-tier applications by connecting frontend, backend, and database containers, ensuring secure and efficient communication.

  3. 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.

Docker Network Types

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:

  1. bridge
  2. host
  3. overlay
  4. ipvlan
  5. macvlan
  6. none

The effects on the docker network depend on the driver used, each driver has a different effect. See below for more information:

  1. bridge: When using the bridge 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.
  2. host: When using the host 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'.
  3. overlay: When using the overlay driver for the docker network the containers within the docker networks are most likely operating on docker swarm, cloud or different hosts/nodes. The overlay driver allows to have interconnectivity between multiple nodes, by allowing the daemons to communicate with each other.
  4. ipvlan: When using the ipvlan 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.
  5. macvlan: When using the macvlan 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.
  6. none: When using the none 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)

Basic Docker Networks Setup

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).

Your Image

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:

  1. Creation of docker network
  2. image pull of phpmyadmin
  3. image pull of mysql
  4. creation of docker volume for phpmyadmin
  5. creation of docker volume for mysql
  6. creation of docker container mysql
  7. creation of docker container phpmyadmin

Creation of Docker Network

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

image pull of phpmyadmin

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

image pull of mysql

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

Creation of Docker Volume for phpmyadmin

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.

Creation of Docker Volume for MySQL

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.

Creation of Docker Container MySQL

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.

Creation of Docker Container phpMyAdmin

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.


Resources

a good resource for this would be the following:

docker network