0

I trigger some Docker containers with Dockerode from within a Node.js app. In some occasions in triggers the error: Error [ERR_STREAM_WRITE_AFTER_END]: write after end (see log below). Is there anything I am doing wrong?

Here is Docker run code:

// Create custom resolver to test data source
const triggerTestDataSourceContainer = () => {
    return async (resolve, source, args, context, resolveInfo) => {
        // [...]

        // Instantiate Docker
        var Docker = require("dockerode");
        var docker = new Docker({ socketPath: "/var/run/docker.sock" });

        // Run Docker container
        docker.run(
            "mobydq-scripts",
            ["python", "run.py", authorization, "test_data_source", dataSourceId.toString()],
            [process.stdout, process.stderr],
            { name: "mobydq-test-data-source-" + dataSourceId, Tty: false, HostConfig: { AutoRemove: true, NetworkMode: "mobydq_network" } },
            function(err, data, container) {
                // Do nothing
            }
        );

        return result;
    };
};

Here is the error:

mobydq-graphql | events.js:180
mobydq-graphql |       throw er; // Unhandled 'error' event
mobydq-graphql |       ^
mobydq-graphql |
mobydq-graphql | Error [ERR_STREAM_WRITE_AFTER_END]: write after end
mobydq-graphql |     at writeAfterEnd (_stream_writable.js:250:14)
mobydq-graphql |     at Socket.Writable.write (_stream_writable.js:299:5)
mobydq-graphql |     at processData (/home/node/app/node_modules/docker-modem/lib/modem.js:354:18)
mobydq-graphql |     at IncomingMessage.processData (/home/node/app/node_modules/docker-modem/lib/modem.js:346:9)
mobydq-graphql |     at IncomingMessage.emit (events.js:203:13)
mobydq-graphql |     at addChunk (_stream_readable.js:295:12)
mobydq-graphql |     at readableAddChunk (_stream_readable.js:276:11)
mobydq-graphql |     at IncomingMessage.Readable.push (_stream_readable.js:210:10)
mobydq-graphql |     at HTTPParser.parserOnBody (_http_common.js:129:22)
mobydq-graphql |     at Socket.socketOnData (_http_client.js:449:22)
mobydq-graphql | Emitted 'error' event at:
mobydq-graphql |     at errorOrDestroy (internal/streams/destroy.js:107:12)
mobydq-graphql |     at writeAfterEnd (_stream_writable.js:252:3)
mobydq-graphql |     at Socket.Writable.write (_stream_writable.js:299:5)
mobydq-graphql |     [... lines matching original stack trace ...]
mobydq-graphql |     at HTTPParser.parserOnBody (_http_common.js:129:22)
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77

1 Answers1

0

Dockerode documentation about the docker.run() method is not very clear, in particular the syntax for create_options and start_options is pretty much not documented. https://github.com/apocas/dockerode#equivalent-of-docker-run-in-dockerode

I guess I was doing something wrong with [process.stdout, process.stderr] and Tty: false argument. It seems the following changes fixed the issue.

const triggerTestDataSourceContainer = () => {
    return async (resolve, source, args, context, resolveInfo) => {
        // [...]

        // Instantiate Docker
        var Docker = require("dockerode");
        var docker = new Docker({ socketPath: "/var/run/docker.sock" });

        // Run Docker container
        docker.run(
            "mobydq-scripts",
            ["python", "run.py", authorization, "test_data_source", dataSourceId.toString()],
            process.stdout,
            { name: "mobydq-test-data-source-" + dataSourceId, HostConfig: { AutoRemove: true, NetworkMode: "mobydq_network" } }, // Start options
            function(err, data, container) {
                // Do nothing
            }
        );

        return result;
    };
};
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77