运行 docker -H ssh://user@hostname 时出现错误,由 Go Exec lib 引起。

huangapple go评论78阅读模式
英文:

Error when running docker -H ssh://user@hostname by Go Exec lib

问题

我正在尝试使用GO远程在树莓派上部署一个容器。以下代码是原始代码的简化片段。

	command := "docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d"

	parsedCommand := parseCommand(command)

	fmt.Println(command)

	cmd := exec.Command(parsedCommand[0], parsedCommand[1:]...)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println(err)
	}

我已经有一个用于建立此连接的SSH密钥,如果我手动运行命令,它可以正常工作,但是通过Go运行时,我遇到了以下错误:

docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d
error during connect: Get "http://docker.example.com/v1.24/containers/json?all=1&filters=%7B%22label%22%3A%7B%22com.docker.compose.project%3Djanus%22%3Atrue%7D%7D": ssh resolves to executable in current directory (./ssh)
exit status 1
英文:

I'm trying to deploy a container on a raspberrypi remotelly usign GO. The following code is a simplified snippet of the original.

	command := "docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d"

	parsedCommand := parseCommand(command)

	fmt.Println(command)

	cmd := exec.Command(parsedCommand[0], parsedCommand[1:]...)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println(err)
	}

I already have a ssh key for making this connection and if i run the commando manually it works well, but running by Go i got this error:

docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d
error during connect: Get "http://docker.example.com/v1.24/containers/json?all=1&filters=%7B%22label%22%3A%7B%22com.docker.compose.project%3Djanus%22%3Atrue%7D%7D": ssh resolves to executable in current directory (./ssh)
exit status 1

答案1

得分: 1

我刚刚在几次测试后自己找到了答案。

错误信息对此并不是很清楚,但我们需要将操作系统环境变量传递给使用cmd.Env = os.Environ()创建的exec.Cmd。

以下是完整的代码示例:

    command := "docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d"

    parsedCommand := parseCommand(command)

    fmt.Println(command)

    cmd := exec.Command(parsedCommand[0], parsedCommand[1:]...)

	cmd.Env = os.Environ()

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    err := cmd.Run()
    if err != nil {
        fmt.Println(err)
    }
英文:

I just found out the answer by my self after a few tests.

The error essage is not very clear about it, but we need to pass the OS env vars to the exec.Cmd that was created with cmd.Env = os.Environ()

here is the full code example:

    command := "docker -H ssh://pi@raspberrypi.local compose -f /tmp/code/docker-compose.yml up -d"

    parsedCommand := parseCommand(command)

    fmt.Println(command)

    cmd := exec.Command(parsedCommand[0], parsedCommand[1:]...)

	cmd.Env = os.Environ()

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    err := cmd.Run()
    if err != nil {
        fmt.Println(err)
    }

huangapple
  • 本文由 发表于 2023年2月24日 02:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定