您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何修改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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。