yumauri / gotenberg-js-client

A simple JS/TS client for interacting with a Gotenberg API
MIT License
111 stars 9 forks source link

Pointing docker url fails #24

Closed AllanPinheiroDeLima closed 3 years ago

AllanPinheiroDeLima commented 3 years ago

I've mounted a dockerfile with all services needed on my server. But the link with gotenberg does not work. Is this the intended behavior ? Below, I'll share my actual code:

docker-compose.yml

services:
  gotenberg:
    image: thecodingmachine/gotenberg:6
    networks:
      - "mps"
    ports:
      - "4000:3000"
  redis:
    image: "redis"
    networks:
      - "mps"
    ports:
      - "6379:6379"
  server:
    image: node:12
    depends_on: 
      - redis
      - gotenberg
    env_file: ".env"
    volumes:
      - "C:\\Users\\allan\\Documents\\github\\mps\\backend\\:/usr/src/app"
    networks:
      - "mps"
    ports:
      - "3001:3001"
      - "9229:9229"
networks:
  mps:
    driver: bridge

And the script I used

        try {
            const convertToPdf = pipe(
                gotenberg("gotenberg"),
                convert,
                office,
                set(filename('result.pdf')),
                please
            )
            const buff = Buffer.from(file.buffer);
            const converted = await convertToPdf({
                [file.originalname]: buff
            });

            return converted
        } catch (err) {
            debugger
        }
    }

Am I doing something wrong ?

yumauri commented 3 years ago

Hello! Gotenberg listens port 3000 by default, and using HTTP protocol, you should define this:

const convertToPdf = pipe(
  gotenberg('http://gotenberg:3000')
  // --8<--

I guess it should work like this, if Docker will resolve name gotenberg inside virtual network.

AllanPinheiroDeLima commented 3 years ago

Yes, it should, but weirdly enough, it didn't work. I took the right steps though.... i have put them in the same network, configured the dependencies correctly. But for some reason, redis does work, but gotenberg does not.

I will try something different now

yumauri commented 3 years ago

Did you change code as I suggested? Because in your code you use just gotenberg("gotenberg"), this is wrong. Also, can you try docker exec bash into container with node application, and use curl here, to connect Gotenberg container from within?

yumauri commented 3 years ago

BTW as far as I know, docker-compose puts all containers from the single docker-compose.yml file into the same virtual network by default, there is no need to do it explicitly.

yumauri commented 3 years ago

Also, maybe Gotenberg needs some time for initialization. Compose's depends_on doesn't wait for service to initialize, it just waits for container to start. If you launch node application right away and trying to connect to Gotenberg immediately — it might be not ready yet.

AllanPinheiroDeLima commented 3 years ago

BTW as far as I know, docker-compose puts all containers from the single docker-compose.yml file into the same virtual network by default, there is no need to do it explicitly.

Yes, the network part is just because of the defaults that company uses on devops.

Did you change code as I suggested? Because in your code you use just gotenberg("gotenberg"), this is wrong. Also, can you try docker exec bash into container with node application, and use curl here, to connect Gotenberg container from within?

I did, but it didn't work either. To be honest, I have tried almost everything

Also, maybe Gotenberg needs some time for initialization. Compose's depends_on doesn't wait for service to initialize, it just waits for container to start. If you launch node application right away and trying to connect to Gotenberg immediately — it might be not ready yet.

I did a health check, but with no success. I will try to setup manually and check back to you

peteralbert commented 3 years ago

@AllanPinheiroDeLima I faced a similar issue: when addressing Gotenberg that ran in Docker directly (e.g. via Postman), everything worked as expected. But with the js client library, I always got a 404 error and Gotenborg was not even reached.

After a long and painful search, I figured that on my local machine were I ran the Gotenberg container, some other app was also running on port 3000! No idea why this did not resulted in some "Address in use" - nor why it worked with Postman - but as soon as changed the port, everything worked smoothly...

yumauri commented 3 years ago

@AllanPinheiroDeLima any updates?

Yesterday I've published version 0.7.0, it has one fix in config merging logic, and introduces new low-level function adjust, with it you can change any internal Request fields.

So, you have two ways for fine tuning:

And you have third, even more powerful way to change HTTP client behaviour: You can change default naive http client with completely your own one, for example, on top of got library. This feature is undocumented, but I can explain in more details, if you need it (in this comment I've written a simple description).

yumauri commented 3 years ago

@AllanPinheiroDeLima I'll close this issue, as far as I reckon it is related to configuration. But feel free to reopen it in case I'm wrong, or any other updates!