0

I am trying to create a container with docker engine's go sdk api. I need to run a command on startup in the container, which I pass as a parameter to Client.ContainerCreate() api. I tried passing that command in different ways, but everytime found some issue. Below is the code I use:

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) If I don't provide Cmd parameter, container is created.

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) If I provide Cmd parameter as Cmd: []string{"/bin/sh"," fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but docker container list doesn't show any container and docker container inspect shows status as exited.

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) If I provide Cmd parameter as Cmd: []string{"sh -c"," fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but ContainerStart() gives "exec: \"sh -c\": executable file not found in $PATH" error. docker container list doesn't show any container.

 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) If I provide Cmd parameter as Cmd: []string{"fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but ContainerStart() gives \"exec: \\"fabric-ca-server start -b admin:adminpw\\": executable file not found in $PATH\": unknown" error. docker container list doesn't show any container.

 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"
        },

There is a .yaml file to create the same container via docker-compose command where sh -c 'fabric-ca-server start -b admin:adminpw' is passed as startup command and the container is created. I am trying to create that container via go code and facing issues.

As I am not seeing any issue with the command in .yaml file, I think command is fine. I am not able to figure out what I am missing. Kindly help :)

Vignesh k
  • 131
  • 2
  • 10

1 Answers1

0

Had posted this question at github and got the answer there. https://github.com/moby/moby/issues/38687

Vignesh k
  • 131
  • 2
  • 10