How to bind using docker http API

352 Views Asked by At

Update

I Think I need to use the .run instead of .createContainer

I got the command to work without a mount however im not sure how to bind docker container to my cwd

    docker.run("wpscanteam/wpscan", [
        // "--mount",
        // "type=bind,source=" + process.cwd() + "/docker-bind,target=/output",
        // "-o",
        // "/output/wpscan-output.json",
        "--format",
        "json",
        "--url",
        "https://example.com/",
    ], process.stdout, function (err, data, container) {
        console.log(data.StatusCode);
    })

The commented lines (2-6) cause errors if uncommented Scan Aborted: invalid option: --mount

I am allowed to add these create arguments and I think I need to use volume but I am not sure.

OG Post

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)

1

There are 1 best solutions below

0
On

This is what I used for the answer

docker.run("wpscanteam/wpscan", [
            "-o",
            "/output/wpscan-output.json",
            "--format",
            "json",
            "--url",
            "https://example.com/",
        ], process.stdout, {
        HostConfig:{
            Binds:[
                process.cwd() + "/docker-bind:/output"
            ]
        }
        }, function (err, data, container) {
            console.log(data.StatusCode);
        }
    )