Skip to content

Commit 4372dfb

Browse files
authored
fix: get containerd socket path (#3521)
1 parent be70d94 commit 4372dfb

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

pkg/cli/create_docker.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,24 +1097,35 @@ func getDockerSocketPath(ctx context.Context) (string, error) {
10971097
}
10981098

10991099
func getContainerdSocketPath(ctx context.Context) (string, error) {
1100-
out, err := exec.CommandContext(ctx, "docker", "run", "-q", "--rm", "--privileged", "--pid=host", "alpine", "nsenter", "-t", "1", "-m", "-p", "-u", "-i", "-n", "sh", "-c", `netstat -xlp | awk '$NF ~ /\/containerd\.sock$/ {print $NF}'`).CombinedOutput()
1100+
// This automatically discards stderr (where the netstat warning lives)
1101+
// and only captures the clean stdout from awk.
1102+
cmd := exec.CommandContext(ctx, "docker", "run", "-q", "--rm", "--privileged", "--pid=host", "alpine", "nsenter", "-t", "1", "-m", "-p", "-u", "-i", "-n", "sh", "-c", `netstat -xlp | awk '$NF ~ /\/containerd\.sock$/ {print $NF}'`)
1103+
out, err := cmd.Output()
1104+
11011105
if err != nil {
1102-
return "", fmt.Errorf("failed to get containerd socket path: %s: %w", string(out), err)
1106+
// Extract stderr for better debugging if the command actually fails
1107+
var exitErr *exec.ExitError
1108+
if errors.As(err, &exitErr) {
1109+
return "", fmt.Errorf("failed to get containerd socket path: %s: %w", string(exitErr.Stderr), err)
1110+
}
1111+
return "", fmt.Errorf("failed to get containerd socket path: %w", err)
11031112
}
11041113

11051114
scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(string(out))))
1106-
containerdSocketPaths := []string{}
11071115
for scanner.Scan() {
1108-
containerdSocketPaths = append(containerdSocketPaths, scanner.Text())
1116+
line := strings.TrimSpace(scanner.Text())
1117+
// Only accept the line if it looks like an absolute path.
1118+
// This ignores any stray warning lines that might still slip through.
1119+
if strings.HasPrefix(line, "/") {
1120+
return line, nil
1121+
}
11091122
}
1123+
11101124
if err := scanner.Err(); err != nil {
1111-
return "", fmt.Errorf("failed to scan containerd socket path: %s: %w", string(out), err)
1112-
}
1113-
if len(containerdSocketPaths) == 0 {
1114-
return "", fmt.Errorf("no containerd socket path found")
1125+
return "", fmt.Errorf("failed to scan containerd socket path: %w", err)
11151126
}
11161127

1117-
return containerdSocketPaths[0], nil
1128+
return "", fmt.Errorf("no containerd socket path found")
11181129
}
11191130

11201131
func convertToMap(config *config.Config) (map[string]interface{}, error) {

0 commit comments

Comments
 (0)