In Docker, there are 3 main types of network modes: bridge, host, and none.

  1. Bridge: A bridge network is the default network mode in Docker. It provides a private network space for containers and enables communication between containers. A bridge network uses a virtual network adapter on the host to connect containers. Each container has its own IP address within the network, and they can communicate with each other using this IP address.
  2. Host: The host network mode removes the network isolation between the host and containers. When a container is in host mode, it uses the host's network stack and shares the host's network namespace. Containers in host mode have access to the host's resources and are visible to the host and other containers on the same network.
  3. None: The none network mode disables all network connectivity for a container. The container will not have an IP address, and it will not be able to communicate with other containers or the host.

Each network mode has its own use cases, and the choice of network mode depends on the requirements of the application and the desired level of network isolation.

 

Bridge mode is the default network mode in Docker, it creates a virtual network adapter on the host machine that connects containers to each other and to the host.

Here's an example of starting a container in bridge mode:

$ docker run -it --network bridge --name container1 busybox

In this example, we're starting a container named container1 and specifying the network mode as bridge using the --network option. The busybox image will be used to launch the container.

Once the container is running, it will be connected to a virtual network adapter on the host and assigned an IP address within the network. This IP address can be used to communicate with other containers on the same network.

You can verify the network configuration of the container using the docker inspect command:

$ docker inspect container1

The output will show the network settings of the container, including the IP address and network mode.

In host mode, a Docker container uses the host's network stack and shares the host's network namespace. The container will have the same network configuration as the host and will be able to access the host's network resources.

Here's an example of starting a container in host mode:

$ docker run -it --network host --name container2 busybox

In this example, we're starting a container named container2 and specifying the network mode as host using the --network option. The busybox image will be used to launch the container.

Once the container is running, it will be using the host's network stack and will have access to the host's network resources, such as network interfaces and ports. Containers in host mode are also visible to the host and other containers on the same network.

You can verify the network configuration of the container using the docker inspect command:

$ docker inspect container2

The output will show the network settings of the container, including the network mode and the IP address, if applicable.

In none mode, a Docker container is not connected to any network and does not have network access. This mode can be used if you don't want the container to have any network connectivity.

Here's an example of starting a container in none mode:

$ docker run -it --network none --name container3 busybox

In this example, we're starting a container named container3 and specifying the network mode as none using the --network option. The busybox image will be used to launch the container.

Once the container is running, it will not have any network connectivity and will not be able to communicate with other containers or the host. The container will not have an IP address.

You can verify the network configuration of the container using the docker inspect command:

ruby

$ docker inspect container3

The output will show the network settings of the container, including the network mode and IP address, if applicable.

Previous Post Next Post