修改或添加映射端口(修改镜像配置)
[toc]
前言
由于一些原因,导致自己开启的端口后面需要用到,所以必须要切换端口映射
上面可以看到MySQL的端口是3306
,我们将会在后面对它改一下,再加上我们之间没有对容器镜像的配置文件进行操作。
下面是修改配置文件 — 修改MySQL的端口映射过程
停止镜像
root@ubuntu:/c# docker stop 846fec372f81
846fec372f81
停止docker服务
root@ubuntu:/c# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
进入docker容器内部文件目录
id是容器id开头的那一长串。我的是Ubuntu系统
root@ubuntu:/var/lib/docker/containers# pwd /var/lib/docker/containers
查找位置,进去
备份原有文件,出现问题后可以还原回去。
cp hostconfig.json hostconfig-bak.json cp config.v2.json config-bak.v2.json
修改hostconfig.json
vim hostconfig.json
修改成想要的端口(可以用
?
或者/
搜索)
这个文件需要了解下配置
如果之前没有端口映射, 应该有这样的一段:
“PortBindings”:{}
增加一个映射, 这样写:
“PortBindings”:{“8888/tcp”:[{“HostIp”:””,“HostPort”:“1888”}]}
**前一个数字是容器端口, 后一个是宿主机端口。**将宿主机的1888端口映射到容器的8888端口 如果增加多个,就是将宿主机的1888和1006端口映射到容器的8888和6006端口
“PortBindings”:{“8888/tcp”:[{“HostIp”:””,“HostPort”:“1888”}],“6006/tcp”:[{“HostIp”:””,“HostPort”:“1006”}]}
而修改现有端口映射更简单, 把端口号改掉就行 如果config.v2.json
里面也记录了端口,也需要修改config.v2.json
下面就是配置文件,看不清没关系,因为我也看不清
ostconfig.json
{"Binds":["/mysql/log:/var/log/mysql","/mysql/data:/var/lib/mysql","/mysql/conf:/etc/mysql/conf.d"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"3306/tcp":[{"HostIp":"","HostPort":"3307"}]},"RestartPolicy":{"Name":"always","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"CgroupnsMode":"host","Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":true,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":["label=disable"],"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":null,"ReadonlyPaths":null}
修改config.v2.json
vim config.v2.json
在下面合适的位置,填写端口信息。
"Ports":{"443/tcp":[{"HostIp":"0.0.0.0","HostPort":"443"}],"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"}]}
启动docker服务
systemctl start docker
启动docker容器
docker start 1f2f202dab24
需要注意的是,docker修改完映射端口后,docker的ip可能会发生变化。
docker开启多个端口
我在网上找资料的时候,发现docker可以开启多个端口,也是改配置文件多加一行的是主机对应的端口
或者启动的时候映射多个端口
docker run -d -p 5000:23 -p 5001:22 --name telnet_ssh --privileged=true telnet:centos /usr/sbin/init
规范写法:
docker run --name definedName(自定义容器名称) \
-p 8080:8080(端口映射 本机端口: docker端口 ) \
-p 8081:8081 \
-p .... \
-d imagesName(镜像名称)