无法在通过docker-engine的go sdk api创建的容器中运行启动命令

时间:2019-02-06 17:30:19

标签: docker docker-api

我正在尝试使用docker引擎的go sdk api创建一个容器。我需要在容器启动时运行命令,并将其作为参数传递给Client.ContainerCreate() api。我尝试以不同的方式传递该命令,但每次都遇到问题。下面是我使用的代码:

resp, err := cli.ContainerCreate(ctx, &container.Config{
                             Image: "hyperledger/fabric-ca",
                             Cmd:   []string{"/bin/sh"," fabric-ca-server start -b admin:adminpw"},
                             Env: []string{"FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server",                    "FABRIC_CA_SERVER_CA_NAME=ca.example.com"}, }, nil, nil, "ca.example.com")
if err != nil {
  fmt.Println(" failed to create container, err:", err)
} else {
  fmt.Println(" Container ID :", resp.ID, "warning:", resp.Warnings, "err:", err)
}

if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
            fmt.Println("failed to start container, err:", err)
    }

1) 如果我不提供Cmd参数,则会创建容器。

Container ID : 02edc80d6545ca2c8089a191ba9174070e1527dc027191e0d7686bff23a9f39d warning: [] err: <nil>

vignesh@vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMES
02edc80d6545        hyperledger/fabric-ca   "/bin/sh -c 'fabric-…"   7 minutes ago       Up 7 minutes        7054/tcp            ca.example.com

2) 如果我将Cmd参数提供为Cmd: []string{"/bin/sh"," fabric-ca-server start -b admin:adminpw"},则ContainerCreate()返回一个容器ID,但是docker container list不显示任何容器,并且docker container inspect显示退出状态。 / p>

Container ID : c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b warning: [] err: <nil>

vignesh@vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vignesh@vignesh-ThinkPad-E470 ~ $ docker container inspect c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b
[
    {
        "Id": "c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b",
        "Created": "2019-02-05T20:05:11.360875853Z",
        "Path": "/bin/sh",
        "Args": [
            " fabric-ca-server start -b admin:adminpw"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 127,
            "Error": "",
            "StartedAt": "2019-02-05T20:05:12.448079587Z",
            "FinishedAt": "2019-02-05T20:05:12.644957269Z"
        },

3)如果我将Cmd参数提供为Cmd: []string{"sh -c"," fabric-ca-server start -b admin:adminpw"},则ContainerCreate()返回一个容器ID,但是ContainerStart()给出了“ exec:\” sh -c \“:可执行文件$ PATH中找不到文件”错误。 docker container list没有显示任何容器。

 Container ID : d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787 warning: [] err: <nil>
failed to start container, err: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"sh -c\": executable file not found in $PATH": unknown

vignesh@vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vignesh@vignesh-ThinkPad-E470 ~ $ docker container inspect d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787
[
    {
        "Id": "d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787",
        "Created": "2019-02-06T17:02:13.989788091Z",
        "Path": "sh -c",
        "Args": [
            " fabric-ca-server start -b admin:adminpw"
        ],
        "State": {
            "Status": "created",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 127,
            "Error": "OCI runtime create failed: container_linux.go:348: starting container process caused \"exec: \\\"sh -c\\\": executable file not found in $PATH\": unknown",
            "StartedAt": "0001-01-01T00:00:00Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },

4)如果我将Cmd参数提供为Cmd: []string{"fabric-ca-server start -b admin:adminpw"},则ContainerCreate()返回一个容器ID,但是ContainerStart()给出\“ exec:\\” fabric-ca-server开始-b admin:adminpw \\“:$ PATH \”中找不到可执行文件:“未知”错误。docker container list不显示任何容器。

 Container ID : d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c warning: [] err: <nil>
failed to start container, err: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"fabric-ca-server start -b admin:adminpw\": executable file not found in $PATH": unknown

vignesh@vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
vignesh@vignesh-ThinkPad-E470 ~ $ docker container inspect d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c
[
    {
        "Id": "d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c",
        "Created": "2019-02-06T17:12:10.443261734Z",
        "Path": "fabric-ca-server start -b admin:adminpw",
        "Args": [],
        "State": {
            "Status": "created",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 127,
            "Error": "OCI runtime create failed: container_linux.go:348: starting container process caused \"exec: \\\"fabric-ca-server start -b admin:adminpw\\\": executable file not found in $PATH\": unknown",
            "StartedAt": "0001-01-01T00:00:00Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },

有一个.yaml文件可通过docker-compose命令创建相同的容器,其中sh -c 'fabric-ca-server start -b admin:adminpw'作为启动命令传递并创建了容器。 我正在尝试通过go代码创建容器并解决问题。

由于我没有看到.yaml文件中命令的任何问题,我认为命令很好。我无法弄清我所缺少的。请帮助:)

1 个答案:

答案 0 :(得分:0)

已经在github上发布了这个问题,并在那里得到了答案。 https://github.com/moby/moby/issues/38687