I was running a mysql container with host mode and here is part of my docker-compose.yml
mysql:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./mysql/datadir:/var/lib/mysql
- ./mysql/mysql-files:/var/lib/mysql-files
- ./mysql/conf.d:/etc/mysql
network_mode: host
container_name: mysql
According to my understanding, host mode will share the host's network, so the port 3306
that mysql container listened in docker should be equal to listened on host's 3306
port. So I should can be access to mysql by 127.0.0.1:3306
or localhost:3306
but the fact is failed.
Why? Is there something wrong?
I can connect to mysql with bridge mode. But for various reasons,I need to use host mode
UPDATE
I found out some clue from this answer How to tell docker on mac to use host network for all purpose?
He said:
The host network driver will work on Linux the way you'd expect, but it will not give you what you are looking for on Mac and Windows.
But this answer was active 3 years ago. I don't know if docker solves this problem now.