Get JSON output of docker container using node

  docker, dockerfile, dockerode, node.js

I am trying to use dockerode to execute this docker command docker run --rm --mount type=bind,source=$HOME/docker-bind,target=/output wpscanteam/wpscan:latest -o /output/wpscan-output.json --format json --url 'https://example.com/'

Here is what I tried however when I run this makes the container with the proper commands however it (almost) immediately exits and when i docker container inspect the contatiner that was created it does not have the mounted volume

i’m not sure Mount is the correct argument I should be using but I don’t think that is the problem here because the docker instance stops in <.5secs and when I run the command in WSL it takes about 10 seconds to run so I don’t think my cmd:[] argument is being executed.

docker.createContainer({
        Image: 'wpscanteam/wpscan',
        AttachStdin: false,
        AttachStdout: true,
        AttachStderr: true,
            Mounts:
                [
                    {
                        "Type": "bind",
                        "Source": process.cwd() + "/docker-bind",
                        "Destination": "/output",
                    }
                ],
        Tty: false,
        Cmd: [
            "-o",
            "/output/wpscan-output.json",
            "--format",
            "json",
            "--url",
            "https://example.com/"
        ],
        OpenStdin: false,
        StdinOnce: false
    }, (err, container)=>{
        container.start(function (err, data) {
            console.log(data)
            console.log(err)
        })
    })

When i run this command docker run --rm --mount type=bind,source=$HOME/docker-bind,target=/output wpscanteam/wpscan:latest -o /output/wpscan-output.json --format json --url 'https://example.com/'
in WSL 2 it works perfectly placing the JSON in the docker-bind folder on my disk.

When converting this command to the dockerode I used process.cwd() instead of $HOME because $HOME is a WSL feature. (I also do have the docker-bind folder in my project directory so that shouldn’t be a problem)

I am fairly experienced with docker however this is wayy beyond my expertise.

tl;dr I have a docker command which I need the output to be placed into a folder within my project directory. It needs to be able to be run using Node.js

If there is a better library that is well supported please let me know.

Docker Docs for you convenience (1.42)

Source: Docker Questions

LEAVE A COMMENT