需求:将slave1的PORTS的宿主机IP改为127.0.0.1,以实现端口仅对本机开放;
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68d8338e3f96 mysql:5.7.24 "docker-entrypoint..." 2 weeks ago Up 2 minutes 33060/tcp, 0.0.0.0:3308->3306/tcp slave2
2278ff4689d0 mysql:5.7.24 "docker-entrypoint..." 6 weeks ago Up 2 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp slave1
第一步:停止docker服务(这里会自动停止所有docker容器)
systemctl stop docker
第二步:修改对应的容器的配置:
先记下docker ps
获取到的CONTAINER ID
,比如slave1对应的CONTAINER ID
是2278ff4689d0
,记录前2到3位即可,如227;
进入到对应的容器所在的目录:(到containers
目录后,输入227再按tab键即可自动补全)
cd /var/lib/docker/containers/2278ff4689d074522499429fb10e0e1cb5937844689874178d722e0f61d3c806/
使用vim修改配置文件hostconfig.json:
{
"Binds":[
"/home/slave1/my.cnf:/etc/my.cnf",
"/home/slave1/mysql:/var/lib/mysql"
],
"ContainerIDFile":"",
"LogConfig":{
"Type":"json-file",
"Config":{
}
},
"NetworkMode":"default",
"PortBindings":{
"3306/tcp":[
{
"HostIp":"127.0.0.1",
"HostPort":"3307"
}
]
},
"RestartPolicy":{
"Name":"always",
"MaximumRetryCount":0
},
"AutoRemove":false,
"VolumeDriver":"",
"VolumesFrom":null,
"CapAdd":null,
"CapDrop":null,
"Dns":[
],
"DnsOptions":[
],
"DnsSearch":[
],
"ExtraHosts":null,
"GroupAdd":null,
"IpcMode":"",
"Cgroup":"",
"Links":[
],
"OomScoreAdj":0,
"PidMode":"",
"Privileged":false,
"PublishAllPorts":false,
"ReadonlyRootfs":false,
"SecurityOpt":null,
"UTSMode":"",
"UsernsMode":"",
"ShmSize":67108864,
"Runtime":"runc",
"ConsoleSize":[
0,
0
],
"Isolation":"",
"CpuShares":0,
"Memory":0,
"NanoCpus":0,
"CgroupParent":"",
"BlkioWeight":0,
"BlkioWeightDevice":null,
"BlkioDeviceReadBps":null,
"BlkioDeviceWriteBps":null,
"BlkioDeviceReadIOps":null,
"BlkioDeviceWriteIOps":null,
"CpuPeriod":0,
"CpuQuota":0,
"CpuRealtimePeriod":0,
"CpuRealtimeRuntime":0,
"CpusetCpus":"",
"CpusetMems":"",
"Devices":[
],
"DiskQuota":0,
"KernelMemory":0,
"MemoryReservation":0,
"MemorySwap":0,
"MemorySwappiness":-1,
"OomKillDisable":false,
"PidsLimit":0,
"Ulimits":null,
"CpuCount":0,
"CpuPercent":0,
"IOMaximumIOps":0,
"IOMaximumBandwidth":0
}
重新启动docker服务:
systemctl start docker
启动容器:
docker start XXX