Skip to content

Commit

Permalink
Fix docker load on systems where context isn't in ~/.docker
Browse files Browse the repository at this point in the history
Signed-off-by: House <[email protected]>
  • Loading branch information
dr-housemd committed Jun 7, 2024
1 parent cc1786a commit 6066348
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 10 additions & 5 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,32 @@ var OCICmd = &cobra.Command{
if loadDocker {
fmt.Println(styles.HighlightStyle.Render("Loading image to docker daemon..."))

expectedInstall := true
currentContext, err := builddocker.GetCurrentContext()
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
expectedInstall = false
}
contextEP, err := builddocker.ReadContextEndpoints()
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
expectedInstall = false
}
if currentContext == "" {
currentContext = "default"
}
if contextEP == nil {
contextEP = make(map[string]string)
}

if _, ok := contextEP[currentContext]; !ok {
contextEP[currentContext] = "unix:///var/run/docker.sock"
}
_ = currentContext

err = oci.LoadDocker(contextEP[currentContext], output+"/result", env.Name)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
if !expectedInstall {
fmt.Println(styles.ErrorStyle.Render("error:", "Is Docker installed?"))
}
os.Exit(1)
}

Expand Down
8 changes: 1 addition & 7 deletions pkg/builddocker/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package builddocker

// source for this file- https://github.com/project-copacetic/copacetic/blob/main/pkg/buildkit/drivers.go
import (
"fmt"
"os"
"path/filepath"

Expand All @@ -13,22 +12,20 @@ import (
func GetCurrentContext() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting user home directory:", err)

return "", err
}

dockerConfigPath := filepath.Join(homeDir, ".docker", "config.json")

data, err := os.ReadFile(dockerConfigPath)
if err != nil {
fmt.Println("Error reading Docker config file:", err)
return "", err
}

// Get the current context from the Docker config file
currentContext := gjson.GetBytes(data, "currentContext").String()
if currentContext == "" {
fmt.Println("No current Docker context found")
return "", err
}

Expand All @@ -40,13 +37,11 @@ func GetCurrentContext() (string, error) {
func ReadContextEndpoints() (map[string]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting user home directory:", err)
return nil, err
}

files, err := os.ReadDir(homeDir + "/.docker/contexts/meta")
if err != nil {
fmt.Println("Error reading Docker context meta directory:", err)
return nil, err
}

Expand All @@ -57,7 +52,6 @@ func ReadContextEndpoints() (map[string]string, error) {
}
data, err := os.ReadFile(homeDir + "/.docker/contexts/meta/" + file.Name() + "/meta.json")
if err != nil {
fmt.Println("Error reading Docker context meta file:", err)
return nil, err
}
contextName := gjson.GetBytes(data, "Name").String()
Expand Down

0 comments on commit 6066348

Please sign in to comment.