docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > Docker容器添加新端口映射

Docker容器添加新端口映射的方法与详细步骤

作者:donglianyou

这篇文章主要给大家介绍了关于Docker容器添加新端口映射的方法与详细步骤,Docker 端口映射即映射容器内应用的服务端口到本机宿主机器,文中通过代码示例介绍的非常详细,需要的朋友可以参考下

在Docker容器已经创建后,需要添加新的端口映射,即对已经存在的Docker容器添加新的端口映射,可以通过以下步骤来添加,即通过修改配置文件的方法。

Windows 10 下 Dockers容器的配置文件修改步骤:

1、先找到要修改的容器hash值:

docker inspect 容器名称

2、然后退出docker Desktop服务

(因为在线状态配置文件修改保存不了)

3、资源管理器中打开最新安装的Docker的配置文件的路径:

\\wsl$\docker-desktop-data\data\docker\containers\[容器hash值] win11一般为: \\wsl.localhost\docker-desktop-data\data\docker\containers\[容器hash值]

4、修改配置文件

打开后修改其中的 config.v2.json 和 hostconfig.json

config.v2.json有两处需要添加,只修改一处,是不行的:

位置1:

"ExposedPorts":{"20/tcp":{},"21/tcp":{},"22/tcp":{},"3306/tcp":{},"443/tcp":{},"6379/tcp":{},"80/tcp":{},"8081/tcp":{},"8082/tcp":{},"8083/tcp":{},"8084/tcp":{},"888/tcp":{},"8888/tcp":{},"9501/tcp":{},"9502/tcp":{}} 

位置2:

"Ports":{"20/tcp":[{"HostIp":"0.0.0.0","HostPort":"1020"}],"21/tcp":[{"HostIp":"0.0.0.0","HostPort":"1021"}],"22/tcp":[{"HostIp":"0.0.0.0","HostPort":"1022"}],"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"13306"}],"443/tcp":[{"HostIp":"0.0.0.0","HostPort":"10443"}],"6379/tcp":[{"HostIp":"0.0.0.0","HostPort":"16379"}],"80/tcp":null,"8081/tcp":[{"HostIp":"0.0.0.0","HostPort":"8081"}],"8082/tcp":[{"HostIp":"0.0.0.0","HostPort":"8082"}],"8083/tcp":[{"HostIp":"0.0.0.0","HostPort":"8083"}],"8084/tcp":[{"HostIp":"0.0.0.0","HostPort":"8084"}],"888/tcp":[{"HostIp":"0.0.0.0","HostPort":"888"}],"8888/tcp":[{"HostIp":"0.0.0.0","HostPort":"8888"}],"9501/tcp":[{"HostIp":"0.0.0.0","HostPort":"9501"}],"9502/tcp":[{"HostIp":"0.0.0.0","HostPort":"9502"}]} 

hostconfig.json 有一处:

"PortBindings":{"20/tcp":[{"HostIp":"","HostPort":"1020"}],"21/tcp":[{"HostIp":"","HostPort":"1021"}],"22/tcp":[{"HostIp":"","HostPort":"1022"}],"3306/tcp":[{"HostIp":"","HostPort":"13306"}],"443/tcp":[{"HostIp":"","HostPort":"10443"}],"6379/tcp":[{"HostIp":"","HostPort":"16379"}],"8081/tcp":[{"HostIp":"","HostPort":"8081"}],"8082/tcp":[{"HostIp":"","HostPort":"8082"}],"8083/tcp":[{"HostIp":"","HostPort":"8083"}],"8084/tcp":[{"HostIp":"","HostPort":"8084"}],"888/tcp":[{"HostIp":"","HostPort":"888"}],"8888/tcp":[{"HostIp":"","HostPort":"8888"}],"9501/tcp":[{"HostIp":"","HostPort":"9501"}],"9502/tcp":[{"HostIp":"","HostPort":"9502"}]} 

5、启动Docker Desktop服务:

点击容器名称 - 点击 Imspect 即可查看到映射的端口列表

补充知识:如何给运行中的docker容器增加映射端口

命令行操作

#1、查看容器的信息
docker ps -a
#2、查看容器的端口映射情况,在容器外执行:
docker port 容器ID 或者 docker port 容器名称
#3、查找要修改容器的全ID
docker inspect 容器ID |grep Id
#4、进到/var/lib/docker/containers 目录下找到与全 Id 相同的目录,修改 其中的hostconfig.json 和 config.v2.json文件:
#注意:若该容器还在运行中,需要先停掉
docker stop 容器ID 或者 docker stop 容器名称
#再停掉docker服务
systemctl stop docker
#可能会提示错误 Warning: Stopping docker.service, but it can still be activated by:
  docker.socket 不要管他 这是docker在关闭状态下被访问自动唤醒机制,很人性化,即这时再执行任意docker命令会直接启动
#5、修改hostconfig.json如下
#	格式如:"{容器内部端口}/tcp":[{"HostIp":"","HostPort":"映射的宿主机端口"}]
"PortBindings":{"22/tcp":[{"HostIp":"","HostPort":"3316"}],"80/tcp":[{"HostIp":"","HostPort":"801"}]}
#6、修改config.v2.json在ExposedPorts中加上要暴露的端口
#	两个地方
"ExposedPorts":{"3306/tcp":{},"80/tcp":{}}"
"Ports":{"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"33061"}],"80/tcp":[{"HostIp":"","HostPort":"801"}]}"

最后改完之后,重启docker服务就行了

systemctl restart docker

总结 

到此这篇关于Docker容器添加新端口映射的方法与详细步骤的文章就介绍到这了,更多相关Docker容器添加新端口映射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文