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!

2
  • Did you check expose/ports docker-compose rules? Commented Jul 13, 2020 at 9:46
  • yes, but ports is ignored with host network mode Commented Jul 14, 2020 at 5:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 - stackoverflow.com/questions/62837078/…
0

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

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

1 Comment

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 - stackoverflow.com/questions/62837078/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.