修改docker容器的端口映射(Mac版)

原创
2019/07/25 11:44
阅读数 7.1K

问题

查看正在运行的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

效果如下图:

确定容器ID

这里的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

具体效果如下图:

获得config.v2.json原内容

复制这个内容,粘贴到本地的文本编辑器上面,格式化进行编辑:

将config.v2.json内容复制到本地vscode

编辑hostconfig.json

cat /var/lib/docker/containers/5f29020b36c0971da5d50825f4132d0eaf40f1e5efaac02eadab23239ca77d7d/hostconfig.json

具体效果如下图:

获得hostconfig.json原内容

复制这个内容,粘贴到本地的文本编辑器上面,格式化进行编辑:

将hostconfig.json内容复制到本地vscode

添加一个新的33060端口映射,如下:

修改hostconfig.json配置

修改完毕之后,将修改后的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修改的内容被还原的问题。

重启docker desktop

重启mysql容器

$ docker start mysql
mysql

检查容器

$ docker container port mysql
33060/tcp -> 0.0.0.0:33060
3306/tcp -> 0.0.0.0:3306

参考

展开阅读全文
加载中

作者的其它热门文章

打赏
1
1 收藏
分享
打赏
5 评论
1 收藏
1
分享
返回顶部
顶部