一、初次创建容器,指定宿主机和容器端口的映射,如下示例:
docker run -itd -p 11935:1935 -p 11985:1985 -p 18080:8080 -p 20903:20903 -p 18888:8888 \
--restart=always \
-v /srv/srs3/conf/:/usr/local/srs/conf/ \
-v /srv/srs3/objs/:/usr/local/srs/objs/ \
centos:7.9.2009
## -i 是让容器的标准输入保持打开状态
## -t 为docker分配一个伪终端并绑定到容器的标准输入上
## -d 表示后台运行容器,可不加前台运行然后ctrl+q+p退出前台
## -p 指定映射端口
## -v 路径映射,主机路径:容器内路径
## 最后是image名称:版本号
二、对已存在运行容器,更改端口
方式一:(删了原来的,根据image重做)
1、停止容器
docker stop 4f4561192346(CONTAINER ID或name 下同)
2、将容器打包成镜像
docker commit 4f4561192346 mycentos:0.1(自定义镜像名:自定版本号)
3、用“一”中的命令,通过新镜像开启新容器
方式二:(直接修改现有容器)
1、停止容器
docker stop 4f4561192346
2、查找要修改的容器配置文件路径
进入到上面Id为名称的路径下
cd /var/lib/docker/containers/4f4561192346fe42a7770eeae5faff92d...
3、停止docker服务(注意:这句开始不要再执行docker命令,否则docker将自动启动服务,导致后面修改配置文件无效!!!停止命令后会有提示)
systemctl stop docker
4、修改名称为hostconfig.json和config.v2.json的两个文件
hostconfig.json(修改1处)
vim config.v2.json(修改2处)
5、重新启动docker
systemctl start docker
6、查看容器端口
docker ps -a
也可以这样看
=== 分隔线,启动后修改容器卷映射
同样修改config.v2.json和hostconfig.json
config.v2.json
"/usr/local/srs/conf/": {
"Source": "/srv/srs/conf/",
"Destination": "/usr/local/srs/conf/",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/srv/srs/conf/",
"Target": "/usr/local/srs/conf/"
},
"SkipMountpointCreation": false
}
hostconfig.json文章来源:https://www.toymoban.com/news/detail-578423.html
文章来源地址https://www.toymoban.com/news/detail-578423.html
"/usr/local/srs/conf": {
"Source": "/srv/srs/conf",
"Destination": "/usr/local/srs/conf",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/srv/srs/conf",
"Target": "/usr/local/srs/conf"
},
"SkipMountpointCreation": false
},
"/usr/local/srs/objs": {
"Source": "/srv/srs/objs",
"Destination": "/usr/local/srs/objs",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/srv/srs/objs",
"Target": "/usr/local/srs/objs"
},
"SkipMountpointCreation": false
}
到了这里,关于Docker中对已存在运行的容器修改端口映射和容器卷映射的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!