'NodeJs Spawn Docker commands

I am trying to run a docker command to backup the database with Node spawn.

The problem is that the process exits with code 1.

If i try this command directly in the console it works.

How should i do?

     const backupProcess = spawn('docker', [
                'exec', container, 'pg_dumpall', `-U ${dbOptions.user}`, '>', `${path}`
            ]);
//container: container name
//dbOptions.user: database user
//path: backup file destination


Solution 1:[1]

You are passing the '>' as argument to docker, but if you execute this in a shell the > is interpreted by the shell and not by docker.

You could try starting a shell wich starts docker and handels the redirect of the output.

const backupProcess = spawn('bash',['-c', `docker exec ${container} pg_dumpall -U ${dbOptions.user} > ${path}`]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1