Docker篇之后续增删端口
在创建容器时,可以用-p来指定端口映射,但是在start容器的时候,并没有有关添加端口映射的方法,无奈百度一下,实践一下,记录一下。
在/var/lib/docker/containers/
下,存在着创建好的容器信息,这些都是我们的容器ID
[root@localhost nginx]# ls /var/lib/docker/containers/ 1f7a9dba7e9ab6792f94b689714288b233e8a67f69433d32759e1778e8e160aa 619a15d3bc06c12383dac6d19f739ed57b79c6b7ae8eff4f0ddcfed9c5856348 320559d876596546b00d443569cf82b0520120fe5212f5155c41beff5c96883b b925118f3b03e224bd97d4a8b28eec06e7f8190688c99acded92df834ed211aa 40b82f229080695467d224a8dabade891246bf1009c14be32896459e1933d798
为了认证这一点,我创建了一个有多个端口映射的新容器
[root@localhost nginx]# docker run -it -d -p10022:22 -p 8004:80 -p 8005:8080 --name centos c5 WARNING: IPv4 forwarding is disabled. Networking will not work. b925118f3b03e224bd97d4a8b28eec06e7f8190688c99acded92df834ed211aa [root@localhost nginx]# docker ps b925118f3b03 c5 "/bin/bash" About an hour ago Up About an hour 0.0.0.0:10022->22/tcp, 0.0.0.0:8004->80/tcp, 0.0.0.0:8005->8080/tcp centos
b925118f3b03e224bd97d4a8b28eec06e7f8190688c99acded92df834ed211aa为容器ID,我们进入容器文件夹内看看,由于目录名过于长,不便观看,我在后面会删除后半部分,希望大家能理解。
[root@localhost nginx]# cd /var/lib/docker/containers/b925118f3b03e22411aa/ [root@localhost b925118f3b03e2b28eea]# ls b925118f3b03e224bd97d4a8b28eec06e7f8190688c99acded92df834ed211aa-json.log config.v2.json hostname mounts resolv.conf.hash checkpoints hostconfig.json hosts resolv.conf
其中config.v2.json
和hostconfig.json
内存储着json格式的信息
[root@localhost b925118f3b03ed211aa]# cat hostconfig.json |grep PortBindings "PortBindings":{"22/tcp":[{"HostIp":"","HostPort":"10022"}],"80/tcp":[{"HostIp":"","HostPort":"8004"}],"8080/tcp":[{"HostIp":"","HostPort":"8005"}]}, [root@localhost b925118f31aa]# cat config.v2.json |grep ExposedPorts "ExposedPorts":{"22/tcp":{},"80/tcp":{},"8080/tcp":{}}
这一段里标识这我们进行端口的映射,注意看格式,并注意添加时逗号,
隔开
"22/tcp":[{"HostIp":"","HostPort":"10022"}] "22/tcp":{}
尝试下删除ssh服务的22端口,首先停止容器要修改的容器。
[root@localhost b9251184ed211aa]# docker stop centos
这是删除完的,注意!!万事修改先备份!!
cp hostconfig.json{,.bak} && cp config.v2.json{,.bak} [root@localhost b925118f3b0ed211aa]# cat hostconfig.json |grep PortBindings "PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"8004"}],"8080/tcp":[{"HostIp":"","HostPort":"8005"}]} [root@localhost b925118df834ed211aa]# cat config.v2.json |grep ExposedPorts "ExposedPorts":{"80/tcp":{},"8080/tcp":{}}
然后重启服务,可以看到之前的22端口映射已经没有了。
[root@localhost b925118f3b03e11aa]# systemctl restart docker [root@localhost b9254ed211aa]# docker ps b925118f3b03 c5 "/bin/bash" About an hour ago Up 4 seconds 0.0.0.0:8004->80/tcp, 0.0.0.0:8005->8080/tcp centos
同理的话也是可以添加的,这里就不示例了,大家可以自己尝试一下。
本帖子也是纯手工制作,转载请标明出处-----------burukku(づ。◕ᴗᴗ◕。)づ