Skip to content

Docker restart support: restart docker container if any settings changed. #1561

@nanoric

Description

@nanoric

Is your feature request related to a problem? Please describe

I'm new to pyinfra and try to use pyinfra to manage docker containers. I found operations.docker then try to use it. But util.docker.diff_from_inspect is not implemented and always returns []. So docker container will never restart unless user specific force=True.

Describe the solution you'd like

There are so many methods to resolve, from simple to complex.

  1. the simpliest: hash all input arguments and add a magic label and check its value to determine to restart or not. For example:
args_hash = hash(want_spec) // so ContainerSpec has the responsibility of normalizing the parameters.
magic_label_name='pyinfra_args_hash'
magic_label = f'{magic_label_name}={args_hash}'
docker_labels = [*user_labels, magic_label]

current_hash = get_fact(....)[...]["Labels"].get('magic_label_name')

container_spec_changes = current_hash != args_hash

recreating = existent_container and (force or container_spec_changes)

Pro: Simpliest and easy to implements ( just adding serval line of code is good enough)
Cons: Only containers created by pyinfra(managed) containers is 'valid', any user created container with the same name will be removed and recreated. (But if everything is created and managed by pyinfra, this will not be a problem).

  1. parse docker inspect carefully, normalize them and compare with existing value.
    Pro: A typed fact.docker.container can be created and used by user.
    Cons: docker inspect's format is complicated and we have to handle the conversion between different syntax(short syntax, long syntax, default fields, etc) manually which has no existing library to use, and some default fields are super hard to get, eg, env_vars.

I prefer method 1 for now because it is easy and robost.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions