Skip to content

Commit

Permalink
fix: docker check on osx incorrectly failing (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass authored May 7, 2024
1 parent 4f8c4cc commit fa460b8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/local/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var dockerClient *docker.Docker
func dockerInstalled(ctx context.Context) (docker.Version, error) {
var err error
if dockerClient == nil {
if dockerClient, err = docker.New(); err != nil {
if dockerClient, err = docker.New(ctx); err != nil {
pterm.Error.Println("Could not create Docker client")
return docker.Version{}, fmt.Errorf("%w: could not create client: %w", localerr.ErrDocker, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/local/local_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewCmdInstall() *cobra.Command {
// only for kind do we need to check the existing port
if provider.Name == k8s.Kind {
if dockerClient == nil {
dockerClient, err = docker.New()
dockerClient, err = docker.New(cmd.Context())
if err != nil {
pterm.Error.Printfln("Could not connect to Docker daemon")
return fmt.Errorf("could not connect to docker: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func defaultHelm(kubecfg, kubectx string) (HelmClient, error) {
RestConfig: restCfg,
})
if err != nil {
return nil, fmt.Errorf("coud not create helm client: %w", err)
return nil, fmt.Errorf("could not create helm client: %w", err)
}

return helm, nil
Expand Down
35 changes: 25 additions & 10 deletions internal/local/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Docker struct {
}

// New returns a new Docker type with a default Client implementation.
func New() (*Docker, error) {
func New(ctx context.Context) (*Docker, error) {
userHome, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not determine user home directory: %w", err)
Expand All @@ -48,20 +48,21 @@ func New() (*Docker, error) {
switch runtime.GOOS {
case "darwin":
// on mac, sometimes the docker host isn't set correctly, if it fails check the home directory
dockerCli, err = client.NewClientWithOpts(append(dockerOpts, client.WithHost("unix:///var/run/docker.sock"))...)
dockerCli, err = createAndPing(ctx, "unix:///var/run/docker.sock", dockerOpts)
if err != nil {
// keep the original error, as we'll join with the next error (if another error occurs)
outerErr := err
// this works as the last WithHost call will win
dockerCli, err = client.NewClientWithOpts(append(dockerOpts, client.WithHost(fmt.Sprintf("unix:///%s/.docker/run/docker.sock", userHome)))...)
if err != nil {
err = fmt.Errorf("%w: %w", err, outerErr)
var err2 error
dockerCli, err2 = createAndPing(ctx, fmt.Sprintf("unix://%s/.docker/run/docker.sock", userHome), dockerOpts)
if err2 != nil {
return nil, fmt.Errorf("%w: could not create docker client: (%w, %w)", localerr.ErrDocker, err, err2)
}
// if we made it here, clear out the original error,
// as we were able to successfully connect on the second attempt
err = nil
}
case "windows":
dockerCli, err = client.NewClientWithOpts(append(dockerOpts, client.WithHost("npipe:////./pipe/docker_engine"))...)
dockerCli, err = createAndPing(ctx, "npipe:////./pipe/docker_engine", dockerOpts)
default:
dockerCli, err = client.NewClientWithOpts(append(dockerOpts, client.WithHost("unix:///var/run/docker.sock"))...)
dockerCli, err = createAndPing(ctx, "unix:///var/run/docker.sock", dockerOpts)
}

if err != nil {
Expand All @@ -71,6 +72,20 @@ func New() (*Docker, error) {
return &Docker{Client: dockerCli}, nil
}

// createAndPing attempts to create a docker client and ping it to ensure we can communicate
func createAndPing(ctx context.Context, host string, opts []client.Opt) (*client.Client, error) {
dockerCli, err := client.NewClientWithOpts(append(opts, client.WithHost(host))...)
if err != nil {
return nil, fmt.Errorf("could not create docker client: %w", err)
}

if _, err := dockerCli.Ping(ctx); err != nil {
return nil, fmt.Errorf("could not ping docker client: %w", err)
}

return dockerCli, nil
}

// Version returns the version information from the underlying docker process.
func (d *Docker) Version(ctx context.Context) (Version, error) {
ver, err := d.Client.ServerVersion(ctx)
Expand Down

0 comments on commit fa460b8

Please sign in to comment.