docke容器启动报错处理

Posted by CnFeat on May 17, 2017

前言

容器启动报错error mounting xxx on var/lib/docker/devicemapper/mnt/xxxx

具体介绍

删除容器里的json文件里的2行替换为空即可。
/var/lib/docker/containers/aac4c5beacb42d1b7070a0549a5b30033e96916bacd2902d5e46a845d8f5ef2b
下面有一个文件叫做:config.v2.json
cat config.v2.json |python -m json.tool
把下面这2行替换即可。
    "MountLabel": "",
    "MountPoints": {},

由于有多个容器,就写了一个批量替换脚本
脚本如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'xie.di'
__mtime__ = '2017/5/16'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃ 永无BUG!   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""

import os
import re
import shutil
import traceback

rootdir = "/var/lib/docker/containers"

for parent, dirname, filename in os.walk(rootdir):
    for f in filename:
        if f == "config.v2.json":
            bak_file = os.path.join(parent, "config.v2.json.bak")
            ori_path = os.path.join(parent, f)

            # 备份文件
            shutil.copy(ori_path, bak_file)

            f_data = open(ori_path).readline()
            mount_pattern = re.compile(r"""(system_u:object_r:svirt_sandbox_file_t:s0:c\d+,c\d+)""")
            process_pattern = re.compile(r"""(system_u:system_r:svirt_lxc_net_t:s0:c\d+,c\d+)""")

            mount_match = re.search(mount_pattern, f_data)
            process_match = re.search(process_pattern, f_data)
            try:
                mount_text = mount_match.group()
                process_text = process_match.group()

                f_data = f_data.replace(mount_text, "")
                f_data = f_data.replace(process_text, "")
                with open(ori_path, "w") as f:
                    f.write(f_data)
            except:
                print "error: {}".format(traceback.format_exc())

更新完毕!!enjoy!