Execute docker commands through a node js script

Nithin D J picture Nithin D J · May 18, 2018 · Viewed 13.4k times · Source

Is there any better way to execute docker commands from a node js apart from using shelljs(similar packages) to execute those commands?

I have seen the package dockerode. Though it is great for some commands, it doesn't give much view on 'docker exec' command.

I just need more control while executing docker exec from nodejs which shelljs fail to provide. I want to know once the docker exec command has been executed whether it got executed successfully or not.

Answer

Nithin D J picture Nithin D J · May 22, 2018

If you want to have run the command from nodejs script, use shelljs or child process to run the command and redirect the output it into a log or txt file. And the use function something like this saerch for error string in the file.

What basically happens is when you execute a docker exec within a cli, even though the error occurs that doesn't captured by shelljs. So the exit code will be 0 either way. So this causes difference in catching error in normal shell command and docker exec command.

We could make use dockerode npm package. We can use this particular example and write it as per our use case. I just changed the code listen to the event 'data' and 'end' on the stream that returned.