A Docker network is a virtual environment enabling communication between containers and the host system. It facilitates the seamless interaction of containerized applications, allowing them to share data and services while providing varying isolation and scalability.
Docker supports several network types:
Bridge network (default): For intra-host container communication.
Host network: For direct access to the host's network stack.
Custom bridge networks: For user-defined configurations and options.
Each network type serves distinct purposes and caters to different use cases, empowering developers to build efficient, distributed, and interconnected containerized applications
When using the host network mode, a container shares the host system's network namespace, which means it uses the same network interfaces and IP address as the host. It can be helpful for specific use cases where we want to access services running on the host from within the container with minimal network isolation.
To create a Docker container using the host network mode, we can use the --network
flag with the value set to host
when running the container. Let’s walk through the step-by-step process of creating a host network mode.
Pull the Docker image that we want to run as a container. For example, we can pull the official Nginx image from Docker Hub:
docker pull nginx
Run the Docker container using the host
network mode:
docker run --name <container_name> --network host -d nginx
We used the --network host
flag to specify the host
network mode and -d
to run the container in detached mode. Replace nginx
with the image name you want to use.
To check the number of containers on the host network in Docker, we can use the docker ps
command along with the --filter
flag to filter containers based on their network mode. Specifically, we can filter containers running in “host” network mode.
docker ps --filter "network=host"
Additionally, if we want to see both running and stopped containers that were previously using the "host" network mode, we can add the --all
or -a
flag to the docker ps
command:
docker ps -a --filter "network=host"
Note: By clicking the "Terminal" widget, execution initiates, pulling the
nginx
image. We replace<container_name>
withtest
, and afterward, we check the number of containers on the host network. Alternatively, you can follow the above commands to manually.