0

I would like to run mysql/mysql-server image in 3 different containers on the same host (End goal is to have innodb cluster running on the host for demo project). And instead of using bridge network and port mapping, I want the containers to start in host network mode.

But, since all 3 containers will be running on same host, I can't have more than one container using same host port.

This is the docker compose file

version: '3'
services:
  mysql:
    image: mysql/mysql-server
    network_mode: "host"

Is it possible to modify docker compose file to achieve my objective? Any pointers would be really helpful!

Chandra Kanth
  • 348
  • 2
  • 13

2 Answers2

1

Host networking is almost never necessary and it disables some core Docker features, like port remapping. If you remove the network_mode: host setting you can pick which host port is used without changing the underlying image at all.

version: '3'
services:
  mysql:
    image: mysql/mysql-server
    ports:
      # You pick the first (host) port.
      # Second port is the port the container process uses,
      # and is usually fixed per image.
      - '3305:3306'

Host networking also disables normal inter-container communication as described in Networking in Compose. Unless you're specifically trying to manage the host's network or you have a truly large number of ports you're listening to (like thousands) you should use the default ("bridge") networking. The only downside is that other containers won't be on the host name localhost, but that should just be a matter of providing appropriate configuration to your service.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I tried with bridge network initially, but had trouble setting up InnoDB cluster with that. If possible, please also have a look at this question - https://stackoverflow.com/questions/62837078/creating-a-mysql-cluster-using-mysql-server-docker-containers-on-multiple-serv – Chandra Kanth Jul 14 '20 at 05:09
0

You can pass --port=3305 in the command.

version: '3'
services:
  mysql:
    image: mysql/mysql-server
    network_mode: "host"
    command: --port=3305

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • thanks! command: --port helped. One more doubt - if the container has 2 services running (ex. mysql on 3306 and mysql-shell on 33060) - how to change ports for both the services? Also, please have a look at this question - https://stackoverflow.com/questions/62837078/creating-a-mysql-cluster-using-mysql-server-docker-containers-on-multiple-serv – Chandra Kanth Jul 14 '20 at 05:08