Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute docker commands through a node js script

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.

like image 740
Nithin D J Avatar asked May 18 '18 10:05

Nithin D J


People also ask

What is Docker JavaScript?

Docker is a tool that allows you package the environment for running your application along with the application itself. You can accomplish this as simply as including a single file called Dockerfile with your project.


2 Answers

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.

like image 126
Nithin D J Avatar answered Sep 24 '22 23:09

Nithin D J


You can use child_process for the exec bash commands and get results to see success or not.

like image 28
S.Polat Avatar answered Sep 22 '22 23:09

S.Polat