feat(docker): add flag to skip if docker is not running

This commit is contained in:
2025-07-03 01:42:42 +03:00
parent 861e257c04
commit b56e934ffc
2 changed files with 29 additions and 0 deletions

View File

@@ -262,6 +262,9 @@ These fields are shared by all installer types. Some fields may vary in behavior
linux: linux/amd64
```
- `opts.skip_if_unavailable`: Whether to skip the installation/update if the Docker daemon is
not running. Defaults to false (so it will fail the installer)
## Installer Examples
All of these examples should be usable, but don't count on them being maintained. Why not look at

View File

@@ -26,6 +26,8 @@ type DockerOpts struct {
Flags *string
// Platform is a platform-specific map of Docker platform strings (e.g., "linux/amd64").
Platform *platform.PlatformMap[string]
// SkipIfUnavailable indicates whether to skip installation if Docker is unavailable.
SkipIfUnavailable *bool
}
// NewDockerInstaller creates a new DockerInstaller.
@@ -45,11 +47,26 @@ func (i *DockerInstaller) Validate() []ValidationError {
// Install implements IInstaller.
func (i *DockerInstaller) Install() error {
if !isDockerAvailable() {
if i.GetOpts().SkipIfUnavailable != nil && *i.GetOpts().SkipIfUnavailable {
logger.Debug("Docker not available, skipping install")
return nil
}
return fmt.Errorf("docker is not available")
}
return i.runOrStartContainer(false)
}
// Update implements IInstaller.
func (i *DockerInstaller) Update() error {
if !isDockerAvailable() {
if i.GetOpts().SkipIfUnavailable != nil && *i.GetOpts().SkipIfUnavailable {
logger.Debug("Docker not available, skipping update")
return nil
}
return fmt.Errorf("docker is not available")
}
image := *i.Info.Name
containerName := i.GetContainerName()
@@ -103,6 +120,9 @@ func (i *DockerInstaller) GetOpts() *DockerOpts {
}
}
}
if skip, ok := (*i.Info.Opts)["skip_if_unavailable"].(bool); ok {
opts.SkipIfUnavailable = &skip
}
return opts
}
@@ -188,3 +208,9 @@ func GetPlatformArchWithFallback(preferred string, fallbacks ...string) string {
}
return preferred
}
// isDockerAvailable checks if Docker is available on the system.
func isDockerAvailable() bool {
err := exec.Command("docker", "info").Run()
return err == nil
}