0

我在 Node.js 应用程序中使用 Dockerode 触发了一些 Docker 容器。在某些情况下会触发错误:(Error [ERR_STREAM_WRITE_AFTER_END]: write after end请参阅下面的日志)。有什么我做错了吗?

这是 Docker 运行代码:

// 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;
    };
};

这是错误:

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)
4

1 回答 1

0

关于该方法的 Dockerode 文档docker.run()不是很清楚,尤其是语法,create_options并且start_options几乎没有记录。 https://github.com/apocas/dockerode#equivalent-of-docker-run-in-dockerode

我想我在争论[process.stdout, process.stderr]中做错了什么。Tty: false似乎以下更改解决了该问题。

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;
    };
};
于 2020-01-30T11:57:07.947 回答