docker容器已经建立并运行, 需要在已有容器上添加新的目录映射,端口映射,环境变量等,我们该如何操作呢?
1. 查找并进入容器配置文件目录
sudobashdirRoot=$(docker info |grep Root|awk '{print $4}')dirHead=$(docker ps|grep postgres|awk '{print $1}')cd$dirRoot/containers/$dirHead*
2. 停止docker服务
systemctl stop docker
3. 编辑config.v2.json 和hostconfig.json文件
使用vim编辑文件并格式化显示json
vim-c":%! python3 -m json.tool" config.v2.json #vim -c ":%! python3 -m json.tool" hostconfig.json
3.1 添加新的目录映射
config.v2.json 中,输入: :/MountPoints 回车, 仿照如下格式进行添加
"/var/log": { "Source": "/var/log", "Destination": "/var/log", "RW": true, "Name": "", "Driver": "", "Type": "bind", "Propagation": "rprivate", "Spec": { "Type": "bind", "Source": "/var/log", "Target": "/var/log" }, "SkipMountpointCreation": false },
hostconfig.json中,输入: /MountPoints 回车, 仿照如下格式进行添加
"/var/log:/var/log",
注意: 如果是数组最后一条记录,后面的 ,逗号都要去掉。
3.2 添加新的端口映射
config.v2.json 中,输入: :/Ports 回车, 在ExpossPorts中添加端口映射信息, 类似如下:
"ExposedPorts": { "5432/tcp": {}, "1883/tcp": {} },
在Ports中添加端口映射信息, 类似如下:
"Ports": { "5432/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "5432" }, { "HostIp": "::", "HostPort": "5432" } ], "1883/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "1883" }, { "HostIp": "::", "HostPort": "1883" } ] },
hostconfig.json中,输入:: /PortBindings 回车, 仿照如下格式进行目录映射添加
"1883/tcp": [ { "HostIp": "", "HostPort": "1883" } ]
3.3 添加新的环境变量
config.v2.json 中,输入: :/Env 回车, 仿照如下格式进行环境变量添加
"Env": [ "APP=yourApp-*.jar", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=en_US.utf8", "PG_MAJOR=13", "PG_VERSION=13.5", "PG_SHA256=9b81067a55edbaabc418aacef457dd8477642827499560b00615a6ea6c13f6b3", "PGDATA=/var/lib/postgresql/data" ],
3.4 添加DNS
hostconfig.json中,输入: :/Dns 回车, 仿照如下格式进行添加
"Dns": [ "114.114.144.114", "8.8.8.8" ],
其他配置的修改类似, 请自行测试验证。
4.重新启动docker服务
systemctl start docker systemctl status docker