summaryrefslogtreecommitdiffstats
path: root/internal/pkg/envcheck/docker.go
blob: f115bc7d838e62c2d53e9d3013daf1913999104e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package envcheck

import (
	"context"
	"fmt"

	"github.com/docker/docker/client"
)

func CheckIfDockerRunning(ctx context.Context, configDockerHost string) error {
	opts := []client.Opt{
		client.FromEnv,
	}

	if configDockerHost != "" {
		opts = append(opts, client.WithHost(configDockerHost))
	}

	cli, err := client.NewClientWithOpts(opts...)
	if err != nil {
		return err
	}
	defer cli.Close()

	_, err = cli.Ping(ctx)
	if err != nil {
		return fmt.Errorf("cannot ping the docker daemon. is it running? %w", err)
	}

	return nil
}