docker容器创建后,如何增加端口映射
在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列会显示你新增的端口。
示例:
你需要将PortBindings节点下,增加一个"{portid}/tcp":[{“HostIp”:"",“HostPort”:"{portid}"}]节点,{portid}需要替换成你新增加的端口号,
“ExposedPorts”:{“443/tcp”:{},“80/tcp”:{}}这里需要添加上需要增加的映射端口。
现在有的办法也就是修改容器的配置文件。
主要有两个文件,一个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列会显示你新增的端口。
示例:
- {"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"]}
- {"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}}