Reputation: 205
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?
Upvotes: 17
Views: 50258
Reputation: 2543
On Fedora 39. First I installed with
sudo dnf install docker docker-compose
docker-compose -v
docker-compose version 1.29.2, build unknown
docker -v
Docker version 24.0.5, build %{shortcommit_cli}
docker was moby-engine
and docker-compose
was older v1 Python version (deprecated). And I had this same error
... "host" network_mode is incompatible with port_bindings
I removed those docker
and docker-compose
and reinstalled docker community edition
sudo dnf remove docker-compose
sudo dnf remove docker
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker version
Client: Docker Engine - Community
Version: 26.1.1
...
docker compose version
Docker Compose version v2.27.0
This installed newer docker-compose
v2 in Go, there was no error anymore. This should be possible on Ubuntu, etc.
Upvotes: 0
Reputation: 24087
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.
Upvotes: 2
Reputation: 11
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?
Upvotes: 1
Reputation: 1230
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.
Upvotes: 1
Reputation: 93
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 .... ....
Upvotes: -1
Reputation: 840
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.
Upvotes: 4
Reputation: 158847
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).
Upvotes: 17