Docker Compose network_mode and port_binding compatibility issue

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?

0

6 Answers

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).

1

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.

1

To access the host's inside your docker, you need to replace:

network_mode: host

with:

ports: - 80:80

You can do the same with any other port.

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 want to connect to a local database then, when connecting to that database, don't use "localhost" or "127.0.0.1". Instead use "host.docker.internal" and that will allow traffic between your container to the database.

1

Get rid of the param ports in your services containing network_mode its like doing mapping twice.

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 .... ....

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like