简体   繁体   中英

Call Docker container outside docker

I have a project running under Apache and without docker. However, in this project, I have a PDF export and this PDF export I want to call it in a docker container. How do you do that?

Here is my 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'

The Browsershot service downloads the PDF and have node and npm. For that, it simulates a browser, so I have a chrome service but I don't know if it's the right method.

These 2 commands works in a terminal :

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

However, Browsershot needs the path of the executable node and npm which are suddenly the 2 commands above just like that :

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

The variable $nodePath is docker-compose run --rm browsershot node and variable $npmPath id docker-compose run --rm browsershot npm . This code works with executable in my local machine like /usr/local/bin/node and /usr/local/bin/npm .

But, when I launch the export with docker commands, I get this error:

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..."

When using docker-compose, I just "ssh" into the container's shell like this:

docker-compose exec php /bin/bash

(where php is the container in my docker-compose.yml )

Rememvber though that for every terminal window you open, you also need to run the following to load in the environment vars:

eval $(docker-machine env)

Without doing that, you'll get the Couldn't connect to Docker daemon message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM