简体   繁体   中英

How to change default port of mysql-server docker image

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!

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM