这篇文章主要讲解了“如何修改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端口上面,即修改已经存在到容器到端口映射配置。
docker container inspect mysql
效果如下图:
这里的mysql容器ID为:5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d
$ docker stop mysql mysql
cd ~/Library/Containers/com.docker.docker/Data/vms/0/
在这个目录下面,有一个tty
的文件,通过这个文件我们能够登录到docker内部的linux界面,然后使用下面命令进行登录:
screen tty
遇到空白命令行,直接回车即可。
cat /var/lib/docker/containers/5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d/config.v2.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"]}
按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引擎,如果跳过这一个步骤就会出现hostconfig.json修改的内容被还原的问题。
$ docker start mysql mysql
$ docker container port mysql 33060/tcp -> 0.0.0.0:33060 3306/tcp -> 0.0.0.0:3306
感谢各位的阅读,以上就是“如何修改docker容器的端口映射”的内容了,经过本文的学习后,相信大家对如何修改docker容器的端口映射这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。