Skip to content

os/exec: exec.Command docker run exit status 1 #14097

@iexploree

Description

@iexploree
package main

/*
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

void executeCmd(const char* cmd)
{
    FILE *fp;
    char buf[1024];
    int readSize;
    int bufSize;

    bufSize = sizeof(buf) - 1;
    fp = popen(cmd, "r");
    if (fp != NULL)
    {
        while(1)
        {
            readSize = fread(buf, sizeof(char), bufSize, fp);
            if(readSize <= 0)
            {
                break;
            }

            buf[readSize] = '\0';
            printf("%s", buf);
        }

        pclose(fp);
    }
    else
        printf("executeCmd popen null:%s\n",strerror(errno));
}
*/
import "C"

import (
    "fmt"
    "os/exec"
    "unsafe"
)

func RunExecCommand() {
    fmt.Println("RunExecCommand")
    bs, err := exec.Command("docker", "run ubuntu:14.04 echo hello").Output()
    if err != nil {
        fmt.Println("docker error:", err)
    } else {
        text := string(bs)
        fmt.Println(text)
    }
}

func RunCPopen() {
    fmt.Println("RunCPopen")

    gostr := "docker run ubuntu:14.04 echo hello"
    cstr := C.CString(gostr)
    C.executeCmd(cstr)
    C.free(unsafe.Pointer(cstr))
}

func main() {
    RunExecCommand()
    RunCPopen()
}

The output is:
RunExecCommand
docker error: exit status 1
RunCPopen
hello

I expect “exec.Command("docker", "run ubuntu:14.04 echo hello").Output()" output is "hello", but it is "docker error: exit status 1". So i write c function "executeCmd" to test it, and it works fine.

My question is why exec.Command is incorrect?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions