Search code examples
node.jsdockerspawn

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

  • 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}`]);