15

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?

Meghna
  • 185
  • 1
  • 2
  • 10

6 Answers6

18

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

David Maze
  • 130,717
  • 29
  • 175
  • 215
5

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.

USMAN FAZIL
  • 757
  • 5
  • 11
  • 1
    I guess with the earlier version of docker-compose the PORTS were accepted in the config, but then ignored. I need network_mode: "host" as I have a docker service listening for udp broadcast messages from boot devices on my home network - specifically Sonos speakers being controlled in node_red. – SteveGroom Aug 30 '21 at 12:50
1

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

network_mode: host

with:

ports:
  - 80:80

You can do the same with any other port.

eightball
  • 970
  • 8
  • 17
1

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?

1

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.

atreeon
  • 21,799
  • 13
  • 85
  • 104
-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 .... ....

kwendo
  • 53
  • 1
  • 6