
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Docker - Networking
Docker networking refers to mechanisms and configurations that enable Docker containers to communicate with each other and the outside world. This allows containers to exchange data, share resources, and interact with external networks like any traditional application that runs on independent machines. Docker provides different networking drivers and features for creating isolated container networks, defining network topologies, and managing network traffic.
Docker networking is essential when creating and running multi-container applications with components talking to each other. This also enables us to connect web servers to database containers or link microservices together in a more complex network architecture. By default, Docker creates a bridge network where all containers on that host are connected over that bridge and can communicate with each other. However, Docker also supports other network drivers for things like overlay networks, such as multi-host communication, and host networks for access to the host's network stack directly.
Types of Networks in Docker
Docker provides several network drivers using which different kinds of networks can be created based on specific requirements. Each driver is unique, offering a different set of features and capabilities that let one tailor communication and isolation between containers.
Driver | Description |
---|---|
bridge | The default network driver. |
host | Remove network isolation between the container and the Docker host. |
none | Completely isolate a container from the host and other containers. |
overlay | Overlay networks connect multiple Docker daemons together. |
ipvlan | IPvlan networks provide full control over both IPv4 and IPv6 addressing. |
macvlan | Assign a MAC address to a container. |
Bridge Network (Default)
- It is the default network created automatically when installing Docker.
- Offers an essential isolation between containers on the same host.
- Each bridge network container has an IP address that allows it to communicate between containers on the same network by their IP addresses or names of the containers.
- It is also possible for containers to use the host's network connection to obtain access to external networks.
Host Network
- It removes the network isolation between the container and the host.
- The container uses the host's network namespace, IP address, and network ports.
- This can be useful if a container needs to access host network interfaces or very low latency is required for network access.
None Network
- It provides complete isolation from all other networks.
- Containers on none network have no network interfaces.
- Handy to use for running containers that do not need any access to the network.
Overlay Network
- It is designed for multi-host networking in a Docker swarm cluster.
- Allows containers running on different Docker hosts to communicate with each other.
- It creates a virtual overlay network across several hosts.
Macvlan Network
- Assigns a MAC address to the container, thus making it appear on the network as if it were an actual device.
- This is useful in scenarios where you need to integrate Docker containers into existing networks that require specific MAC addresses or when the containers are supposed to communicate directly with some physical devices.
IPvlan Network
- It provides finer control of IPv4 and IPv6 addressing for containers.
- It provides different modes like L2 or L3 to serve different isolation and routing needs.
The choice of the correct network driver should be determined by the specific use case, desired level of isolation, level of communication, and integration with outer networks.
Important Docker Network Commands
To manage networking in a Docker container, there are several network commands you can use to create, manage, and inspect networks. These commands can be used to configure and maintain the connectivity in Docker containers. Here are some of the key Docker network commands −
Create a Network
You can use the docker network create command to create a Docker network. It allows you to specify the driver and options for the network.
$ docker network create my_network

With this command, you can also specify the network driver (e.g., bridge, overlay).
$ docker network create --driver bridge my_bridge_network

List Networks
If you want to list all the networks on your host, you can use the docker network ls command. The output of this command will be a list of all the networks along with their names and drivers.
$ docker network ls

Inspect a Network
When working with networks in Docker, you may need to fetch information related to the networks in your host. The docker network inspect command helps you to fetch detailed information related to a specific network.
It provides important details such as the networks configuration, connected containers, and IP ranges.
$ docker network inspect my_network

Connect a Container to a Network
If you already have a network created and you want to connect it to a running container, you can use the docker network connect command. This will allow a container to communicate with other containers on the network mentioned in the command.
$ docker network connect my_network my_container

Disconnect a Container from a Network
If a network is associated with a container and you want to disconnect it from the container, you can use the docker network disconnect command. It will remove the containers connection to the specified network.
$ docker network disconnect my_network my_container

Remove a Network
If a network is no longer needed, you can remove it from the system using the docker network rm command. With the help of this command, you can remove only those networks that are not currently in use by any containers.
$ docker network rm my_network

Prune Unused Networks
If you want to remove all the unused Docker networks to free up resources, you can use the docker network prune command. Before removing all the networks, this command will let you be double-sure by prompting you for confirmation.
$ docker network prune

Additional Network Commands
Create an Overlay Network
$ docker network create --driver overlay my_overlay_network
Create a Macvlan Network
$ docker network create --driver macvlan --subnet 192.168.1.0/24 --gateway 192.168.1.1 -o parent=eth0 my_macvlan_network

Conclusion
In this chapter, we have discussed in detail about the Docker networks. We have understood the different types of network drivers in Docker and how to use the various Docker network commands to manage container connectivity. These commands allow you to create, configure, and manage networks within Docker environments.
FAQs on Docker Networking
1. How do I connect containers to the same network in Docker?
You can do that either by passing the --network flag to the docker run command or by configuring a network in your docker-compose.yml. You would simply need to use the name of the network you want to attach your container to when calling with the --network flag. In Docker Compose, you add a network section and define the configuration.
2. What is the difference between bridge and overlay networks in Docker?
Bridge networks are the default ones in Docker and provide straightforward communication between containers on the same host based on IPs. Overlay networks are used in Swarm mode to enable communication between containers across multiple hosts - each swarm creates a virtual network that spans across the swarm.
3. How can I expose a port on a Docker container?
You can expose a port from a Docker container using the p or --publish option when starting a container. The format is simply hostPort:containerPort, where you want to expose the hostPort on the host machine, and the containerPort is what the application listens to in the container.
4. How do I troubleshoot networking issues in Docker?
If you are having some issues with Docker networking, you would want to begin by checking your network configuration of the containers with Docker inspect. You can also check the container logs for any error messages related to networking. If necessary, you can use the docker network commands to list, inspect, and diagnose problems with your Docker networks.