在 docker 外调用 Docker 容器

Call Docker container outside docker

我在 Apache 下有一个项目 运行,但没有 docker。但是,在这个项目中,我有一个 PDF 导出,这个 PDF 导出我想在 docker 容器中调用它。你是怎么做到的?

这是我的 docker-compose.yaml :

version: "3.3"

services:
    php:
        image: registry.gitlab.com/nodevo/keneo/php:latest
        volumes:
            - .:/srv:rw,cached
            - ~/.ssh/id_rsa:/root/.ssh/id_rsa:ro
            - ~/.composer:/tmp

    browsershot:
        image: ouranoshong/browsershot
        links:
          - chrome

    chrome:
        build: ./docker/chrome
        cap_add:
            - SYS_ADMIN
        ports:
            - '9223:9222'

Browsershot服务下载PDF,有node和npm。为此,它模拟了一个浏览器,所以我有一个 chrome 服务,但我不知道它是否是正确的方法。

这 2 个命令在终端中有效:

docker-compose run --rm browsershot node -v
docker-compose run --rm browsershot npm -v

但是,Browsershot 需要可执行节点的路径和 npm,它们突然是上面的 2 个命令:

/**
 * @param string $nodePath
 * @param string $npmPath
 */
public function __construct(string $nodePath, string $npmPath)
{
    $this->browserShot = new Browsershot();
    $this->browserShot
        ->setNodeBinary($nodePath)
        ->setNpmBinary($npmPath)
        ->showBackground()
        ->setOption('fullPage', true)
        ->setOption('args', ['--disable-web-security'])
        ->emulateMedia('screen')
    ;
}

变量$nodePathdocker-compose run --rm browsershot node,变量$npmPathid是docker-compose run --rm browsershot npm。 此代码适用于我本地计算机中的可执行文件,例如 /usr/local/bin/node/usr/local/bin/npm.

但是,当我使用 docker 命令启动导出时,出现此错误:

HTTP 500 Internal Server Error
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`docker-compose run --rm browsershot node docker-compose run --rm browsershot npm root -g` docker-compose run --rm browsershot node '/media/vdufour/DATA/Sites/keneo/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/639306024-0718333001579852859\/index.html","action":"pdf","options":{"args":["--disable-web-security"],"viewport":{"width":800,"height":600},"fullPage":true,"emulateMedia":"screen","delay":2000,"displayHeaderFooter":false,"printBackground":true}}'" failed.

Exit Code: 1(General error)

Working directory: /media/vdufour/DATA/Sites/keneo/public

Output:
================


Error Output:
================
Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

尝试 运行 命令 bash

docker-compose run --rm browsershot bash -c "npm && node && some_command..."

当使用 docker-compose 时,我只是 "ssh" 进入容器的 shell 像这样:

docker-compose exec php /bin/bash

(其中 php 是我 docker-compose.yml 中的容器)

请记住,对于您打开的每个终端 window,您还需要 运行 以下内容以在环境变量中加载:

eval $(docker-machine env)

如果不这样做,您将收到 Couldn't connect to Docker daemon 消息。