diff --git a/cmd/diagnostics.go b/cmd/diagnostics.go index a6d0650..973fde9 100644 --- a/cmd/diagnostics.go +++ b/cmd/diagnostics.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/pterodactyl/wings/environment" "io" "io/ioutil" "net/http" @@ -19,7 +20,6 @@ import ( "github.com/AlecAivazis/survey/v2/terminal" "github.com/docker/cli/components/engine/pkg/parsers/operatingsystem" "github.com/docker/docker/api/types" - "github.com/docker/docker/client" "github.com/docker/docker/pkg/parsers/kernel" "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/system" @@ -187,7 +187,7 @@ func diagnosticsCmdRun(cmd *cobra.Command, args []string) { } func getDockerInfo() (types.Version, types.Info, error) { - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + cli, err := environment.DockerClient() if err != nil { return types.Version{}, types.Info{}, err } diff --git a/environment/docker.go b/environment/docker.go index b30bd8d..9165e37 100644 --- a/environment/docker.go +++ b/environment/docker.go @@ -3,6 +3,7 @@ package environment import ( "context" "github.com/apex/log" + "sync" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" @@ -10,10 +11,28 @@ import ( "github.com/pterodactyl/wings/config" ) +var _cmu sync.Mutex +var _client *client.Client + +// Return a Docker client to be used throughout the codebase. Once a client has been created it +// will be returned for all subsequent calls to this function. +func DockerClient() (*client.Client, error) { + _cmu.Lock() + defer _cmu.Unlock() + + if _client != nil { + return _client, nil + } + + _client, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()); + + return _client, err +} + // Configures the required network for the docker environment. func ConfigureDocker(c *config.DockerConfiguration) error { // Ensure the required docker network exists on the system. - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + cli, err := DockerClient() if err != nil { return err } diff --git a/environment/docker/environment.go b/environment/docker/environment.go index 4893032..2a0d7aa 100644 --- a/environment/docker/environment.go +++ b/environment/docker/environment.go @@ -55,7 +55,7 @@ type Environment struct { // reference the container from here on out. This should be unique per-server (we use the UUID // by default). The container does not need to exist at this point. func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) { - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + cli, err := environment.DockerClient() if err != nil { return nil, err } diff --git a/server/install.go b/server/install.go index f6bdaa1..2666fb7 100644 --- a/server/install.go +++ b/server/install.go @@ -129,7 +129,7 @@ func NewInstallationProcess(s *Server, script *api.InstallationScript) (*Install ctx, cancel := context.WithCancel(context.Background()) s.installer.cancel = &cancel - if c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()); err != nil { + if c, err := environment.DockerClient(); err != nil { return nil, errors.WithStack(err) } else { proc.client = c