2

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.

afraid.jpg
  • 965
  • 2
  • 14
  • 31
  • Connect from where? Since you're disabling Docker's networking layer, you won't be able to easily connect to this database from other containers. (I'm not sure what host networking gives you that `ports: [3306:3306]` wouldn't also achieve.) – David Maze Jul 30 '21 at 11:19

2 Answers2

2

What rights has the login you use ? Docker has its own way to manage network so if your login use 'localhost' or 127.0.0.1', it is not what your container will see. Try docker inspect <yourContainerId> | grep IPAddress and you will see the 'real' address of you container inside the docker network.

If you give rights to your login@'%' it should work then.

Dedey
  • 247
  • 1
  • 8
0

Check the following,

  1. Host networking is supported on Linux hosts only.
  2. Check the host firewall settings for port 3306, if the port is open and available.
  3. Check the ip address with ip addr show
  4. Verify which process is bound to port 80, using the netstat command. You need to use sudo because the process is owned by the Docker daemon user and you otherwise will not be able to see its name or PID.

sudo netstat -tulpn | grep :80

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5