简体   繁体   English

使用主机网络运行具有不同端口的多个 mysql 容器

[英]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.我有一个使用默认端口 3306 在主机上运行的 Mysql 服务器。我想使用网络主机运行一个 MySQL docker 容器,但使用不同的端口。

My configuration is defined in a docker-compose file.我的配置是在 docker-compose 文件中定义的。 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.如果使用主机网络是一项硬性要求,那么 Docker 空间中的任何内容都无法控制或限制服务使用或不使用哪些端口。 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;特别是对于侦听单个 TCP 端口的服务器(如大多数数据库和基于 HTTP 的服务器),默认的 Docker 网桥/NAT 设置应该可以正常工作; alternate setups like host networking and macvlan are unnecessary.不需要像主机网络和 macvlan 这样的备用设置。 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.这里 1000 是您要运行 mysql docker 容器的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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