My docker-compose.yml contains this:
version: '3.2'
services:
mysql:
image: mysql:latest
container_name: mysql
restart: always
network_mode: "host"
hostname: localhost
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
volumes:
- $HOME/data/datasql:/var/lib/mysql
ports:
- 3306:3306
user-management-service:
build: user-management-service/
container_name: user-management-service
restart: always
depends_on:
- mysql
- rabbitmq
- eureka
network_mode: "host"
hostname: localhost
ports:
- 8089:8089
When I try to do docker-compose up, I get the following error:
"host" network_mode is incompatible with port_bindings
Can anyone help me with the solution?
Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine. The application inside the container can be accessed using a port at the host's IP address (e.g., port 80).
If you set a predictable project name for the first composition you can use external_links to reference external containers by name from a different compose file. In the next docker-compose release (1.6) you will be able to use user defined networks, and have both compositions join the same network.
If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.
Compose command compatibility with docker-compose The compose command in the Docker CLI supports most of the docker-compose commands and flags. It is expected to be a drop-in replacement for docker-compose.
Docker Compose Bridge Networking. Docker Compose is an easy way for deploying multi-container applications. It automates a lot of the booking keeping, networking and resource management of applications in a single neat docker-compose.yml file. You can get the app up by running docker-compose up and turn it back down using docker-compose down.
When deploying a Compose application on an Docker Engine with Swarm mode enabled , you can make use of the built-in overlay driver to enable multi-host communication. Consult the Swarm mode section, to see how to set up a Swarm cluster, and the Getting started with multi-host networking to learn about multi-host overlay networks.
Your openvpn service has a 1194:1194/udp port binding. This is used to bind a port of the host to a port of the container. But as you used the host networking mode, the container will use the host's networking interfaces, making your port bindings useless as the container will have access to the host's ports.
network_mode: host
is almost never necessary. For straightforward servers, like the MySQL server you show or what looks like a normal HTTP application, it's enough to use normal (bridged) Docker networking and ports:
, like you show.
If you do set up host networking, it completely disables Docker's networking stack. You can't call to other containers using their host name, and you can't remap a container's port using ports:
(or choose to not publish it at all).
You should delete the network_mode:
lines you show in your docker-compose.yml
file. The container_name:
and hostname:
lines are also unnecessary, and you can delete those too (specific exception: RabbitMQ needs a fixed hostname:
).
I feel like the two places I see host networking are endorsed are either to call back to the host machine (see From inside of a Docker container, how do I connect to the localhost of the machine?), or because the application code has hard-coded localhost
as the host name of the database or other components (in which case Docker and a non-Docker development setup fundamentally act differently, and you should configure these locations using environment variable or another mechanism).
Quick solution:
Downgrade the docker-compose version and you'll be fine. The issue is with the latest docker-compose version and network_mode: "host"
I faced the same issue on v1.29.2 and while everything worked smooth on v1.27.4.
I had the same problem with network_mode: 'host'.
When downgrading docker-compose from 1.29.2 to 1.25.4, it worked fine. Maybe some bug added in new versions?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With