Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't ask Gotenberg to fetch url from docker with multi-domain app #116

Closed
Pierstoval opened this issue Sep 27, 2019 · 4 comments · Fixed by #155
Closed

Can't ask Gotenberg to fetch url from docker with multi-domain app #116

Pierstoval opened this issue Sep 27, 2019 · 4 comments · Fixed by #155
Labels
enhancement New feature or request

Comments

@Pierstoval
Copy link

Hello folks!

I've just discovered Gotenberg and I configured it in my docker-compose.yaml file, the exposed port is okay, and I can generate PDFs from remote URLs.

However, it only works with remote urls.

Let me explain.

I have a PHP application which must be multidomain.

Let's consider such Compose config:

services
    app:
        # It exposes port 8000, and requests on "wrong" domains such as 127.0.0.1 return HTTP 400 error

    gotenberg:
        image: thecodingmachine/gotenberg:5

I'll need to generate PDFs from the app container, therefore I will execute something like this from inside this container:

$ curl --request POST \
    --url http://gotenberg:3000/convert/url \
    --header "Content-Type: multipart/form-data" \
    --form remoteURL=http://mydomain.docker:8000/something.html \
    -o result.pdf

However, this will not work: gotenberg's container doesn't know anything about the mydomain.docker host, so I end up having a blank page in result.pdf.
By the way, any curl call to the same address will result in a curl: (7) Failed to connect to mydomain.docker port 8000: Connection refused error.

I even tried echo "mydomain.docker app" >> /etc/hosts, without success (doesn't seem to be taken in account at all).

I could find a hack that would have worked with Curl, by "mocking" the host:

$ curl http://app:8000/ -H"Host: mydomain.docker"

Using this trick, curl will effectively call the app host (the same as the app's container name, valid inside a Docker network), but the resolved host will be mydomain.docker. It just works with curl.

However, it does not go beyond as this trick does not seem to be possible with Gotenberg.

If it was possible to use Gotenberg without a separate Docker container, it would probably be easier, as I could directly install it in the app container and make all queries "from inside" (it would make the image heavier but would be worth it IMO). But I'm not sure copy/pasting config and setup from your repository would be worth it nor maintainable.

I also tried using the /html endpoint, but there are so many CSS, JS and images in the web page that I don't even know how I can achieve that without hundreds of lines of code full of hacks or overengineered stuff.

What I'm almost sure about is that it would probably work in production. I don't use any "complex" setup (just have one dedicated server) so running the docker image alongside all other apps seems pretty okay, and as it would call totally remote addresses (with proper DNS-based hosts), it would work without harm. But for dev, it's not possible.

I'm starting to think that I won't be able to use Gotenberg at all with the setup I have...

Do you have any idea of how I could solve this?

As a side-note, I have chromium installed on the main Docker image (necessary to use Panther for testing).
Should I look further there?

@LoicHa
Copy link

LoicHa commented Sep 28, 2019

I manage to do it like this, using the container name,
(i use php , but this is the same stuff for CURL )

$client = new Client('http://got_pdf:4000', new \Http\Adapter\Guzzle6\Client());
DockerCompose file
image

I hope it helps

@gulien
Copy link
Collaborator

gulien commented Sep 29, 2019

Hello @Pierstoval

In a Docker Compose network, the hostname of a service is the service name directly.
In your case I think the value of remoteURL should be something like http://app:8000/something.html.

@Pierstoval
Copy link
Author

@LoicHa The problem I have is not related to how I connect to Gotenberg from my PHP client or with Curl, because I already use the Gotenberg container's name as host and I can call it. The problem is that Gotenberg can't connect to my PHP app because of how domain names are handled.

@gulien as said, when I do this, Gotenberg will effectively call http://app:8000/something.html , but in this call, the host will be app, therefore my PHP app returns 404 because the host name must be mydomain.docker.

This could be solved if we could customize how Gotenberg itself makes the call to the container. As said, if it used curl for example, I could add options to curl to make sure the called hostname is app and the Host HTTP header could be mydomain.docker. But Gotenberg doesn't seem to provide any entrypoint to how it makes HTTP calls when using the /convert/url converter.

Maybe Gotenberg could propose a new form option that would contain a JSON string with an http field in order to customize how HTTP calls are made, such as allowing to add HTTP headers for example.

@gulien
Copy link
Collaborator

gulien commented Sep 30, 2019

Indeed, being able to set custom headers for remoteURL (and webhookURL) form fields seems like a useful feature 😄

Related with #81.

A workaround solution would be to use some kind of "proxy":

Gotenberg => proxy (send request with correct headers) => app

@gulien gulien added the enhancement New feature or request label Sep 30, 2019
@gulien gulien added this to the 6.1.0 milestone Oct 2, 2019
@gulien gulien modified the milestones: 6.1.0, 6.2.0 Oct 21, 2019
@gulien gulien modified the milestones: 6.2.0, 6.1.0 Nov 18, 2019
This was referenced Dec 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants