微信扫一扫 分享朋友圈

已有 863 人浏览分享

开启左侧

docker容器创建后,如何增加端口映射

[复制链接]
863 0
在docker容器创建之后,想要增加端口映射,一般来说都是将原有的容器导出为镜像,然后删除原有容器,在重新创建的时候增加端口映射,但是这样操作起来太过于麻烦了。
现在有的办法也就是修改容器的配置文件。
主要有两个文件,一个hostconfig.json,一个config.v2.json.
路径主要是在/var/lib/docker/containers下。

1.首先将所有的容器都停止,docker stop {container_id/name} 有多少个就执行多少个,否则后面修改的配置文件会不生效,并且记住你要停止的docker容器的id的前几位(3-5位即可)

2.所有容器都停止之后,停止docker,systemctl stop docker

3.进入到/var/lib/docker/containers/{container_id}/ ,然后将hostconfig.json,config.v2.json做个备份,
接着就是来修改了。

4.在打开的文件中,查找到端口映射的节点,不好找的话,可以查找一下你之前做过的端口号,在这个节点下,增加一个端口节点,也就是把你之前做的一个映射端口的内容复制下来,将里面的端口号修改为你需要增加的端口即可,确保json格式无误,保存。记住,hostconfig.json和config.v2.json两个文件都必须修改正确。

5.重启docker,启动容器,不出意外,现在你的docker容器上的ports列会显示你新增的端口。
示例:
  1. {"Binds":["/home/huangxl/all_configs/docker/config/nginx/conf.d:/etc/nginx/conf.d/","/home/huangxl/all_configs/docker/config/nginx/nginx.conf:/etc/nginx/nginx.conf","/home/huangxl/all_configs/docker/logs/nginx:/var/log/nginx/","/home/huangxl/workspaces/php:/var/www/html"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"docker-net","PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"443"}],"80/tcp":[{"HostIp":"","HostPort":"80"}]},"RestartPolicy":{"Name":"always","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"CgroupnsMode":"private","Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"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":[],"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":null,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
复制代码
你需要将PortBindings节点下,增加一个"{portid}/tcp":[{“HostIp”:"",“HostPort”:"{portid}"}]节点,{portid}需要替换成你新增加的端口号,
  1. {"StreamConfig":{},"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2022-01-24T08:10:43.679849992Z","FinishedAt":"2022-01-24T08:10:56.742066426Z","Health":null},"ID":"88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35","Created":"2022-01-24T02:32:12.827291831Z","Managed":false,"Path":"nginx","Args":["-g","daemon off;"],"Config":{"Hostname":"88e1e9ea182a","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"443/tcp":{},"80/tcp":{}},"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NGINX_VERSION=1.15.12-1~stretch","NJS_VERSION=1.15.12.0.3.1-1~stretch"],"Cmd":["nginx","-g","daemon off;"],"Image":"nginx:1.15","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"},"StopSignal":"SIGTERM"},"Image":"sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf","NetworkSettings":{"Bridge":"","SandboxID":"6cfe5beae192b849becbd6afbbe301ef0d85cc4d1182bbe3d9176558d5b001bb","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"docker-net":{"IPAMConfig":null,"Links":null,"Aliases":["88e1e9ea182a"],"NetworkID":"aaa7dcf058dc099459b8575fad9904b4417106b4d76ceb1da76bc2227b708219","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":null,"SandboxKey":"/var/run/docker/netns/6cfe5beae192","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35/88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35-json.log","Name":"/nginx","Driver":"overlay2","OS":"linux","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":true,"MountPoints":{"/etc/nginx/conf.d":{"Source":"/home/huangxl/all_configs/docker/config/nginx/conf.d","Destination":"/etc/nginx/conf.d","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/huangxl/all_configs/docker/config/nginx/conf.d","Target":"/etc/nginx/conf.d/"},"SkipMountpointCreation":false},"/etc/nginx/nginx.conf":{"Source":"/home/huangxl/all_configs/docker/config/nginx/nginx.conf","Destination":"/etc/nginx/nginx.conf","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/huangxl/all_configs/docker/config/nginx/nginx.conf","Target":"/etc/nginx/nginx.conf"},"SkipMountpointCreation":false},"/var/log/nginx":{"Source":"/home/huangxl/all_configs/docker/logs/nginx","Destination":"/var/log/nginx","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/huangxl/all_configs/docker/logs/nginx","Target":"/var/log/nginx/"},"SkipMountpointCreation":false},"/var/www/html":{"Source":"/home/huangxl/workspaces/php","Destination":"/var/www/html","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/huangxl/workspaces/php","Target":"/var/www/html"},"SkipMountpointCreation":false}},"SecretReferences":null,"ConfigReferences":null,"AppArmorProfile":"docker-default","HostnamePath":"/var/lib/docker/containers/88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35/hostname","HostsPath":"/var/lib/docker/containers/88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/88e1e9ea182a19fbd258b7146f18d65a8ab95aca9199392c26c7e3af2f1b8f35/resolv.conf","SeccompProfile":"","NoNewPrivileges":false,"LocalLogCacheMeta":{"HaveNotifyEnabled":false}}
复制代码
“ExposedPorts”:{“443/tcp”:{},“80/tcp”:{}}这里需要添加上需要增加的映射端口。
免责声明:
1,海欣资源网所发布的资源由网友上传和分享,不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
2,海欣资源网的资源来源于网友分享,仅限用于学习交流和测试研究目的,不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
3,海欣资源网所发布的资源由网友上传和分享,版权争议与本站无关,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。
4,如果您喜欢,请支持正版,购买正版,得到更好的正版服务,如有侵权,请联系我们删除并予以真诚的道歉,联系方式邮箱 haixinst@qq.com
海欣资源-企业信息化分享平台。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

0

关注

0

粉丝

24

主题
热度排行
回复排行
最新贴子

Archiver|手机版|海欣资源 ( 湘ICP备2021008090号-1 )|网站地图

GMT+8, 2024-11-21 18:00 , Gzip On, MemCached On.

免责声明:本站所发布的资源和文章均来自网络,仅限用于学习交流和测试研究目的,不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。 本站信息来自网络,版权争议与本站无关,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 如果您喜欢,请支持正版,购买正版,得到更好的正版服务,如有侵权,请联系我们删除并予以真诚的道歉。