Keywords: Docker Compose | Network Mode | Version Compatibility | Container Network | Configuration Syntax
Abstract: This article provides an in-depth analysis of the evolution of network mode configuration in Docker Compose files, focusing on the syntax change from net to network_mode. Through practical cases, it demonstrates the correct method for configuring host network mode in Docker Compose v2/v3 versions, and explains version compatibility issues and their solutions in detail. Combining Q&A data and reference articles, it offers complete configuration examples and best practice recommendations.
Background of Docker Compose Network Configuration Evolution
In the Docker ecosystem, network configuration has always been a critical aspect of containerized application deployment. As Docker and Docker Compose versions continue to iterate, related configuration syntax has undergone significant evolution. Early Docker Compose configuration files used the net parameter to define container network modes, which was the standard practice at the time.
Version Compatibility Issue Analysis
According to the error information provided by the user, using net: "host" in the configuration file with Docker 1.10.2 and Docker Compose 1.6.0 environments causes validation failure. The error message clearly indicates: Unsupported config option for services.app: 'net'. This suggests that in newer Docker Compose versions, the net parameter has been deprecated or replaced.
Correct Configuration Syntax
In Docker Compose v2 and later configuration files, the correct configuration parameter for network mode is network_mode. Here is the corrected configuration example:
version: '2'
services:
mysql:
image: mysql
network_mode: "host"
nginx:
image: nginx
network_mode: "host"
app:
image: tomcat
network_mode: "host"
Version Evolution and Documentation Updates
The documentation link referenced by the user points to Docker 1.6 version documentation, while the actual Docker Compose version used is 1.6.0. This version mismatch is the main cause of configuration errors. In the official Docker Compose v2/v3 documentation, network_mode is explicitly used as the configuration parameter for network modes.
Detailed Explanation of Network Mode Options
The network_mode parameter supports multiple network mode configurations:
network_mode: "bridge"- Default bridge network modenetwork_mode: "host"- Use host machine's network stacknetwork_mode: "none"- Disable network functionalitynetwork_mode: "service:[service name]"- Share network stack with other servicesnetwork_mode: "container:[container name/id]"- Share network stack with specified container
Practical Application Scenario Analysis
In the case provided by the reference article, the user encountered network access issues when using an Ubuntu virtual machine. By configuring network_mode: host, containers were able to directly use the host machine's network stack, thereby accessing resources within the VPN network. This fully demonstrates the value of host network mode in practical deployments.
Configuration Considerations
When using network_mode: host, attention should be paid to:
- Containers will directly use the host machine's network namespace
- Port mapping configuration (ports) is typically not needed in this mode
- Network isolation between containers will be affected
- Suitable for scenarios requiring direct access to host machine network resources
Version Compatibility Best Practices
To ensure configuration compatibility, it is recommended to:
- Always refer to the official documentation for the corresponding version
- Use
network_modein Docker Compose v2 and later versions - Regularly update Docker and Docker Compose to stable versions
- Standardize Docker environment versions within teams
Summary and Recommendations
Through the analysis in this article, it can be seen that Docker Compose network configuration syntax has evolved from net to network_mode. Understanding this evolution is crucial for correctly configuring container networks. In practical applications, appropriate network modes should be selected based on specific requirements, and configuration syntax should be matched with Docker Compose versions.