英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论