栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

修改docker容器启动时执行的命令

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

修改docker容器启动时执行的命令

docker容器时指定了command,后期要修改可通过修改对应容器的config.v2.json来配置

import os, json


####################################################################################
# Options that must be configured

# docker container name
# The data type is string, example:
# name = 'HelloApp'
name = 'HelloApp'

# The command to execute when the docker container starts
# The data type is list, example:
# command = [u'python', u'/codes/HelloApp/index.py']
command = [u'python', u'/codes/HelloApp/index.py']

# Disk location of docker container
# The data type is string, example:
# docker_container_path = '/var/lib/docker/containers'
docker_container_path = '/var/lib/docker/containers'

####################################################################################
# The following parts should not be changed at will !!!!!!!!!!!!
#
# get docker container id
result = os.popen("docker ps --no-trunc|grep %s" % name)
res = result.read()
if res:
    id_arr = res.split('  ')
    if len(id_arr) > 2:
        container_id = id_arr[0]
if container_id:
    # format config.v2.json path
    config_path = docker_container_path + "/%s/config.v2.json" % container_id
    if os.path.exists(config_path):
        with open(config_path, 'r') as fr:
            config = json.loads(fr.read())

if config:
    # stop docker service
    os.system("systemctl stop docker.socket")
    os.system("systemctl stop docker")
    
    # modfiy config
    config[u'Args'] = command
    config[u'Config'][u'Cmd'] = [u'nohup'] + command
    # write to config file
    with open(config_path, 'w') as fw:
        fw.write(json.dumps(config))
    
# start docker service
os.system("systemctl start docker")

####################################################################################
# END


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/599654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号