Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 694
Meghna Avatar asked Jun 03 '21 10:06

Meghna


People also ask

What is Network_mode host in Docker compose?

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

How do I connect two Docker composes?

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.

Can Docker containers interact with each other?

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.

Is compose compatible with docker-compose?

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.

What is bridge networking in 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.

How do I enable multi-host communication in Docker Compose?

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.

How to bind a port to a container using OpenVPN?

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.


Video Answer


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

like image 63
David Maze Avatar answered Oct 23 '22 11:10

David Maze


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.

like image 24
USMAN FAZIL Avatar answered Oct 23 '22 11:10

USMAN FAZIL


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?

like image 27
Saoussen BOUDHIAFI Avatar answered Oct 23 '22 09:10

Saoussen BOUDHIAFI