Skip to content

How to return value from executing a command from container? #729

@chill37

Description

@chill37

I've used your example on running container exec.
https://github.com/apocas/dockerode/blob/v3.3.5/examples/exec_running_container.js

in the log, i can see the result of the execution, but i want to be able to use the result. But right now, it looks like it's just a log.

my code:

async a() {
    const docker = new Docker(networkConfig.ipfs);
    const dockerode = docker.getDockerode();
    const container = dockerode.getContainer('xxx');
    const id = await this.runExec(container);
    console.log("id"+id);
}

async runExec(container: any): Promise<any> {
        const options = {
            Cmd: ['ipfs', 'id'],
            AttachStdin: true,
            AttachStdout: true,
            AttachStderr: true
        };
        const attach_opts = {
            stream: true,
            stdin: true,
            stdout: true,
            stderr: true
        };
        try {
            const exec = await container.exec(options);
            const stream = await exec.start(attach_opts);
            const aa = await container.modem.demuxStream(stream, process.stdout, process.stderr);
            const data = await exec.inspect();
            console.log("data: "+data);
            return data;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

result is:

data:
{
  ID: '28468c484a1cb4d3bb20446d99ce8930f34b12fb0f18b31482c496d3d6b7c833',
  Running: true,
  ExitCode: null,
  ProcessConfig: {
    tty: false,
    entrypoint: 'ipfs',
    arguments: [ 'id' ],
    privileged: false
  },
  OpenStdin: true,
  OpenStderr: true,
  OpenStdout: true,
  CanRemove: false,
  ContainerID: '4a798e6f7c0228179e39e9118cba13169db9c4978f4c78556f4abfaf689ab9d5',
  DetachKeys: '',
  Pid: 0
}

id:
{
  ID: '489d727549dc740669282cad70f912baeac3723c959effcdc8f9330191d9fdd4',
  Running: true,
  ExitCode: null,
  ProcessConfig: {
    tty: false,
    entrypoint: 'ipfs',
    arguments: [ 'id' ],
    privileged: false
  },
  OpenStdin: true,
  OpenStderr: true,
  OpenStdout: true,
  CanRemove: false,
  ContainerID: '4a798e6f7c0228179e39e9118cba13169db9c4978f4c78556f4abfaf689ab9d5',
  DetachKeys: '',
  Pid: 0
}
{
        "ID": "12D3KooWSzQfouQU1U8Wm2nmrgxuoM5bQVq7GSW6RJjtB7bLKH7q",
        "PublicKey": "CAESIP8pUFmDcym4V8gTff1NR2ybaBqbVa1jkzE3xYTyIduM",
        "Addresses": [
                "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWSzQfouQU1U8Wm2nmrgxuoM5bQVq7GSW6RJjtB7bLKH7q",
                "/ip4/192.168.0.2/tcp/4001/p2p/12D3KooWSzQfouQU1U8Wm2nmrgxuoM5bQVq7GSW6RJjtB7bLKH7q"
        ],
        "AgentVersion": "kubo/0.19.1/958e586/docker",
        "ProtocolVersion": "ipfs/0.1.0",
        "Protocols": [
                "/ipfs/bitswap",
                "/ipfs/bitswap/1.0.0",
                "/ipfs/bitswap/1.1.0",
                "/ipfs/bitswap/1.2.0",
                "/ipfs/id/1.0.0",
                "/ipfs/id/push/1.0.0",
                "/ipfs/lan/kad/1.0.0",
                "/ipfs/ping/1.0.0",
                "/libp2p/autonat/1.0.0",
                "/libp2p/circuit/relay/0.2.0/stop",
                "/x/"
        ]
}

the last json containing ID, Publickey, etc. is what i need, but that is not part of the returned value from runExec function.
is there a way to receive this result as a return value of a runExec function?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions