From 8920c4a170603dc8f23e9e0d4ba0a9de13cd5f94 Mon Sep 17 00:00:00 2001 From: Christopher Homberger Date: Thu, 28 Aug 2025 17:28:08 +0000 Subject: [PATCH] Timeout to wait for and optionally require docker always (#741) * do not require docker cli in the runner image for waiting for a sidecar dockerd * allow to specify that `:host` labels would also only be accepted if docker is reachable Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/gitea/act_runner/pulls/741 Reviewed-by: Lunny Xiao Co-authored-by: Christopher Homberger Co-committed-by: Christopher Homberger --- internal/app/cmd/daemon.go | 31 ++++++++++++++++++++++++- internal/pkg/config/config.example.yaml | 4 ++++ internal/pkg/config/config.go | 20 +++++++++------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index d478dbb..06810b2 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -5,6 +5,7 @@ package cmd import ( "context" + "errors" "fmt" "os" "path" @@ -13,6 +14,7 @@ import ( "slices" "strconv" "strings" + "time" "connectrpc.com/connect" "github.com/mattn/go-isatty" @@ -64,7 +66,34 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu log.Warn("no labels configured, runner may not be able to pick up jobs") } - if ls.RequireDocker() { + if ls.RequireDocker() || cfg.Container.RequireDocker { + // Wait for dockerd be ready + if timeout := cfg.Container.DockerTimeout; timeout > 0 { + tctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + keepRunning := true + for keepRunning { + dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost) + if err != nil { + log.Errorf("Failed to get socket path: %s", err.Error()) + } else if err = envcheck.CheckIfDockerRunning(tctx, dockerSocketPath); errors.Is(err, context.Canceled) { + log.Infof("Docker wait timeout of %s expired", timeout.String()) + break + } else if err != nil { + log.Errorf("Docker connection failed: %s", err.Error()) + } else { + log.Infof("Docker is ready") + break + } + select { + case <-time.After(time.Second): + case <-tctx.Done(): + log.Infof("Docker wait timeout of %s expired", timeout.String()) + keepRunning = false + } + } + } + // Require dockerd be ready dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost) if err != nil { return err diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 67b7db5..8fe4448 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -99,6 +99,10 @@ container: force_pull: true # Rebuild docker image(s) even if already present force_rebuild: false + # Always require a reachable docker daemon, even if not required by act_runner + require_docker: false + # Timeout to wait for the docker daemon to be reachable, if docker is required by require_docker or act_runner + docker_timeout: 0s host: # The parent directory of a job's working directory. diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 17147f0..93e8206 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -45,15 +45,17 @@ type Cache struct { // Container represents the configuration for the container. type Container struct { - Network string `yaml:"network"` // Network specifies the network for the container. - NetworkMode string `yaml:"network_mode"` // Deprecated: use Network instead. Could be removed after Gitea 1.20 - Privileged bool `yaml:"privileged"` // Privileged indicates whether the container runs in privileged mode. - Options string `yaml:"options"` // Options specifies additional options for the container. - WorkdirParent string `yaml:"workdir_parent"` // WorkdirParent specifies the parent directory for the container's working directory. - ValidVolumes []string `yaml:"valid_volumes"` // ValidVolumes specifies the volumes (including bind mounts) can be mounted to containers. - DockerHost string `yaml:"docker_host"` // DockerHost specifies the Docker host. It overrides the value specified in environment variable DOCKER_HOST. - ForcePull bool `yaml:"force_pull"` // Pull docker image(s) even if already present - ForceRebuild bool `yaml:"force_rebuild"` // Rebuild docker image(s) even if already present + Network string `yaml:"network"` // Network specifies the network for the container. + NetworkMode string `yaml:"network_mode"` // Deprecated: use Network instead. Could be removed after Gitea 1.20 + Privileged bool `yaml:"privileged"` // Privileged indicates whether the container runs in privileged mode. + Options string `yaml:"options"` // Options specifies additional options for the container. + WorkdirParent string `yaml:"workdir_parent"` // WorkdirParent specifies the parent directory for the container's working directory. + ValidVolumes []string `yaml:"valid_volumes"` // ValidVolumes specifies the volumes (including bind mounts) can be mounted to containers. + DockerHost string `yaml:"docker_host"` // DockerHost specifies the Docker host. It overrides the value specified in environment variable DOCKER_HOST. + ForcePull bool `yaml:"force_pull"` // Pull docker image(s) even if already present + ForceRebuild bool `yaml:"force_rebuild"` // Rebuild docker image(s) even if already present + RequireDocker bool `yaml:"require_docker"` // Always require a reachable docker daemon, even if not required by act_runner + DockerTimeout time.Duration `yaml:"docker_timeout"` // Timeout to wait for the docker daemon to be reachable, if docker is required by require_docker or act_runner } // Host represents the configuration for the host.