0

我有一个在 Apache 下运行且没有 docker 的项目。但是,在这个项目中,我有一个 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 服务,但我不知道它是否是正确的方法。

这两个命令在终端中工作:

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')
    ;
}

变量$nodePathisdocker-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.
4

2 回答 2

0

尝试使用 bash 运行命令

docker-compose run --rm browsershot bash -c "npm && node && some_command..."
于 2020-01-28T15:54:49.550 回答
0

使用 docker-compose 时,我只是像这样“SSH”到容器的外壳中:

docker-compose exec php /bin/bash

php我的容器在哪里docker-compose.yml

请记住,对于您打开的每个终端窗口,您还需要运行以下命令来加载环境变量:

eval $(docker-machine env)

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

于 2020-01-28T16:44:54.590 回答