Docker 容器 RestartCount 未递增

Docker container RestartCount not incrementing

测试

def test_can_pop_new_container(self):
    config = {
        'ip': '10.49.0.2',
        'subnet': '10.49.0.0/16',
        'gateway': '10.49.0.202',
        'vlan': 102,
        'hostname': 'test-container',
    }
    container = container_services.pop_new_container(config, self.docker_api)

    inspection = self.docker_api.inspect_container(container.get('Id'))
    print('before', inspection.get('RestartCount'), inspection.get('StartedAt'))
    container_services.restart(container, self.docker_api)
    new_inspection = self.docker_api.inspect_container(container.get('Id'))
    print('after', new_inspection.get('RestartCount'), new_inspection.get('StartedAt'))

代码

def restart(container, docker_client):
    return docker_client.restart(container.get('Id'))

输出

通过测试我得到

before 0 None
after 0 None

来自 docker ps 确认容器已重新启动。

CONTAINER ID        IMAGE                                                 COMMAND                  CREATED             STATUS              PORTS               NAMES
86f16438ffdd        docker.akema.fr:5000/coaxis/coaxisopt_daemon:latest   "/usr/bin/supervis..."   28 seconds ago      Up 17 seconds                           confident_dijkstra

问题

那为什么RestartCount还在0?我是否使用了错误的字段?

正如评论中已经指出的那样,RestartCount 字段用于 Restart Policies 的上下文中,以在出现故障时跟踪重新启动尝试。

如果用户启动重启,它不会增加。

你可以看看docker events to keep track on normal container restarts. This is also available for dockerpy.