swanchain / go-computing-provider

A golang implementation of computing provider
MIT License
21 stars 22 forks source link

Can't start resource-exporter version 11.2.9 #135

Open cuongdt1994 opened 3 weeks ago

cuongdt1994 commented 3 weeks ago

Can't start container resource-exporter version 11.2.9 with computing-provider version 0.6.4.

Error log:

time="2024-08-23 10:57:56.455" level=info msg="Starting a computing-provider client." func=func19 file="ubi.go:166" time="2024-08-23 10:57:56.502" level=error msg="restartResourceExporter failed, error: create resource-exporter container failed, error: Error response from daemon: invalid restart policy: maximum retry count can only be used with 'on-failure'" func=func19 file="ubi.go:177" time="2024-08-23 10:57:56.514" level=info msg="Your config file is:/home/ecp/config.toml" func=func19 file="ubi.go:183" time="2024-08-23 10:57:57.631" level=info msg="CP service started successfully, listening on port: 9085" func=func19 file="ubi.go:212"

with docker ps -a

root@ecp:/home/ecp# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Normalnoise commented 2 weeks ago

please follow the faq to upgrade the resource-exporter: https://docs.swanchain.io/computing-provider/edge-computing-provider-ecp/ecp-faq#q-how-do-i-update-the-resource-exporter-in-ecp

lushdog commented 2 weeks ago

@Normalnoise It's useless. The docker container config is wrong. The ContainerCreateAndStart code is

err = dockerService.ContainerCreateAndStart(&container.Config{
        Image:        build.UBIResourceExporterDockerImage,
        AttachStdout: true,
        AttachStderr: true,
        Tty:          true,
    }, &container.HostConfig{
        RestartPolicy: container.RestartPolicy{
            Name:              container.RestartPolicyAlways,
            MaximumRetryCount: 3,
        },
        Privileged: true,
    }, resourceExporterContainerName)

The code you provided is attempting to create and start a Docker container using the Go programming language and the Docker client. The specific issue is with the restart policy configuration in the ContainerCreateAndStart function.

The error you’re encountering:

maximum retry count cannot be used with restart policy 'always'

is due to the combination of the RestartPolicyAlways policy with a MaximumRetryCount, which is not allowed. The RestartPolicyAlways policy is intended to restart the container indefinitely, without considering retry counts.

Solution To resolve this error, you should modify the restart policy in the ContainerCreateAndStart function. Here are two possible solutions:

  1. Remove the MaximumRetryCount with the RestartPolicyAlways: If you want the container to always restart regardless of the exit status, remove the MaximumRetryCount field.

Modify the HostConfig section in your code as follows:

err = dockerService.ContainerCreateAndStart(&container.Config{
    Image:        build.UBIResourceExporterDockerImage,
    AttachStdout: true,
    AttachStderr: true,
    Tty:          true,
}, &container.HostConfig{
    RestartPolicy: container.RestartPolicy{
        Name: container.RestartPolicyAlways,
        // Remove MaximumRetryCount as it's incompatible with 'always'
    },
    Privileged: true,
}, resourceExporterContainerName)
  1. Use the on-failure Restart Policy with a Maximum Retry Count: If you want the container to restart only when it fails (i.e., exits with a non-zero status) and to limit the number of restarts, change the policy to on-failure and keep the MaximumRetryCount.

Modify the HostConfig section in your code as follows:

err = dockerService.ContainerCreateAndStart(&container.Config{
    Image:        build.UBIResourceExporterDockerImage,
    AttachStdout: true,
    AttachStderr: true,
    Tty:          true,
}, &container.HostConfig{
    RestartPolicy: container.RestartPolicy{
        Name:              "on-failure",
        MaximumRetryCount: 3,  // This will now work with 'on-failure'
    },
    Privileged: true,
}, resourceExporterContainerName)

Summary The choice between the two options depends on your requirements: If you want the container to restart indefinitely regardless of how it exits, use always without a retry count. If you want to limit retries only on failure, use on-failure with the MaximumRetryCount. Adjust the code accordingly based on which behavior best suits your application's needs!

sonic-chain commented 2 weeks ago

Thank you for your suggestion. This bug has been fixed in the releases branch.

Normalnoise commented 1 week ago

@cuongdt1994 please follow this guideline:https://github.com/swanchain/go-computing-provider/issues/137