From 9e4a5f7363a5559ddaa3a48e91bb8e69db861de4 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sun, 18 Jun 2023 05:38:38 +0000 Subject: [PATCH] feat: improve Docker configuration and detection handling (#242) - Pass `cfg` to `envcheck.CheckIfDockerRunning` function - Add `Docker` struct to `config.go` for Docker configuration - Update `config.example.yaml` with `docker` configuration options - Modify `CheckIfDockerRunning` in `docker.go` to use Docker host from config if provided Signed-off-by: appleboy Reviewed-on: https://gitea.com/gitea/act_runner/pulls/242 Reviewed-by: Lunny Xiao Reviewed-by: wxiaoguang Co-authored-by: appleboy Co-committed-by: appleboy --- internal/app/cmd/daemon.go | 2 +- internal/pkg/config/config.example.yaml | 6 ++++++ internal/pkg/config/config.go | 6 ++++++ internal/pkg/envcheck/docker.go | 15 ++++++++++++--- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index d9b1821..aebe641 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -63,7 +63,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, } if ls.RequireDocker() { - if err := envcheck.CheckIfDockerRunning(ctx); err != nil { + if err := envcheck.CheckIfDockerRunning(ctx, cfg); err != nil { return err } } diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index f692dd0..46bf295 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -68,3 +68,9 @@ container: # valid_volumes: # - '**' valid_volumes: [] + +docker: + # overrides the docker client host with the specified one. + # default value is the value of DOCKER_HOST environment variable. + # if DOCKER_HOST is not set, the default value is unix:///var/run/docker.sock + host: "" diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index eb916fe..11ebd16 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -50,12 +50,18 @@ type Container struct { ValidVolumes []string `yaml:"valid_volumes"` // ValidVolumes specifies the volumes (including bind mounts) can be mounted to containers. } +// Docker represents the configuration for Docker. +type Docker struct { + Host string `yaml:"host"` // Host specifies the Docker host. +} + // Config represents the overall configuration. type Config struct { Log Log `yaml:"log"` // Log represents the configuration for logging. Runner Runner `yaml:"runner"` // Runner represents the configuration for the runner. Cache Cache `yaml:"cache"` // Cache represents the configuration for caching. Container Container `yaml:"container"` // Container represents the configuration for the container. + Docker Docker `yaml:"docker"` // Docker represents the configuration for Docker. } // LoadDefault returns the default configuration. diff --git a/internal/pkg/envcheck/docker.go b/internal/pkg/envcheck/docker.go index 841ca23..c657fec 100644 --- a/internal/pkg/envcheck/docker.go +++ b/internal/pkg/envcheck/docker.go @@ -7,12 +7,21 @@ import ( "context" "fmt" + "gitea.com/gitea/act_runner/internal/pkg/config" + "github.com/docker/docker/client" ) -func CheckIfDockerRunning(ctx context.Context) error { - // TODO: if runner support configures to use docker, we need config.Config to pass in - cli, err := client.NewClientWithOpts(client.FromEnv) +func CheckIfDockerRunning(ctx context.Context, cfg *config.Config) error { + opts := []client.Opt{ + client.FromEnv, + } + + if cfg.Docker.Host != "" { + opts = append(opts, client.WithHost(cfg.Docker.Host)) + } + + cli, err := client.NewClientWithOpts(opts...) if err != nil { return err }