如何修改docker容器的端口映射
这篇文章主要讲解了“如何修改docker容器的端口映射”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何修改docker容器的端口映射”吧!
成都创新互联服务项目包括赤峰林西网站建设、赤峰林西网站制作、赤峰林西网页制作以及赤峰林西网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,赤峰林西网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到赤峰林西省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
问题
查看正在运行的MySQL的docker容器端口映射情况:
$ docker container port mysql 3306/tcp -> 0.0.0.0:3306 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f29020b36c0 mysql "docker-entrypoint.s…" 16 hours ago Up 33 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
从上面两条命令查询结果,可知,mysql的3306端口映射到本机物理机的3306端口上面了,现在的问题是想要把mysql容器的33060端口,也映射到本机物理机到33060端口上面,即修改已经存在到容器到端口映射配置。
步骤
确定容器id
docker container inspect mysql
效果如下图:
这里的mysql容器ID为:5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d
停止容器
$ docker stop mysql mysql
登录docker内部的linux
cd ~/Library/Containers/com.docker.docker/Data/vms/0/
在这个目录下面,有一个tty
的文件,通过这个文件我们能够登录到docker内部的linux界面,然后使用下面命令进行登录:
screen tty
遇到空白命令行,直接回车即可。
编辑config.v2.json
cat /var/lib/docker/containers/5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d/config.v2.json
具体效果如下图:
复制这个内容,粘贴到本地的文本编辑器上面,格式化进行编辑:
编辑hostconfig.json
cat /var/lib/docker/containers/5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d/hostconfig.json
具体效果如下图:
复制这个内容,粘贴到本地的文本编辑器上面,格式化进行编辑:
添加一个新的33060端口映射,如下:
修改完毕之后,将修改后的json压缩成一行,通过vi修改hostconfig.json文件:
vi /var/lib/docker/containers/5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d/hostconfig.json
内容如下:
{"Binds":["/Users/zhangyalin/Documents/databases/mysql/data:/var/lib/mysql"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"3306/tcp":[{"HostIp":"","HostPort":"3306"}],"33060/tcp":[{"HostIp":"","HostPort":"33060"}]},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"DNS":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"shareable","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,"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":0,"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"]}
退出linux
按Ctrl+A,然后快速按D,退出screen命令。然后,使用screen -ls
命令,查看当前会话:
$ screen -ls There is a screen on: 31169.ttys003.zylMBP (Detached) 1 Socket in /var/folders/lb/yvhs6lhx3wz8d5mdgw4w81xr0000gn/T/.screen.
再使用kill命令杀死会话:
kill -9 31169
彻底退出会话:
screen -wipe There is a screen on: 31169.ttys003.zylMBP (Removed) 1 socket wiped out. No Sockets found in /var/folders/lb/yvhs6lhx3wz8d5mdgw4w81xr0000gn/T/.screen.
重启docker desktop
一定需要重启docker引擎,如果跳过这一个步骤就会出现hostconfig.json修改的内容被还原的问题。
重启mysql容器
$ docker start mysql mysql
检查容器
$ docker container port mysql 33060/tcp -> 0.0.0.0:33060 3306/tcp -> 0.0.0.0:3306
感谢各位的阅读,以上就是“如何修改docker容器的端口映射”的内容了,经过本文的学习后,相信大家对如何修改docker容器的端口映射这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
新闻名称:如何修改docker容器的端口映射
链接分享:http://pwwzsj.com/article/gdjijs.html