简体   繁体   中英

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.

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
docker run --name 'dockername' -e MYSQL_ROOT_PASSWORD='password' -p 1000:3306 -d mysql
docker exec -it 'dockername' mysql -uroot -p
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'
flush privileges;

here 1000 is the port at which you want to run your mysql docker container.

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