Runing multiple mysql containers with different port using host network

I have a Mysql server running on the host using default port 3306. I Want to run a MySQL docker container using network host but with a different port.

My configuration is defined in a docker-compose file. After building the image and tried running the container, it starts and shutdown with port conflict notice.

Is there a way to dynamically change the container port before starting up? I don’t want to use the network bridge.

Answer

If using host networking is a hard requirement, then nothing in Docker space will be able to control or limit what ports the service does or doesn’t use. You need to change a service-specific configuration file, environment variable, or command-line argument to make it listen somewhere else.

Particularly for servers that listen on a single TCP port (like most database and HTTP-based servers) the default Docker bridge/NAT setup should work fine; alternate setups like host networking and macvlan are unnecessary. If you’re willing to use the standard setup, this is trivial:

version: '3'
services:
  mysql:
    image: mysql
    ports: ['9999:3306'] # listen on host port 9999 instead

Leave a Reply

Your email address will not be published. Required fields are marked *