Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get or post data from API with Drupal app served with Docker Compose

I have some apps running locally via. docker for development. 2 days ago they were working fine, but yesterday a bug came out. I use curl to get data from an API served on a remote host, and this stopped working yesterday, and is still not working. The curl command to get data is

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, getenv('DATABASE_URL'));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_VERBOSE, 0);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
      curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));  
      $output = curl_exec($ch)
      curl_close($ch);
      return $output;

which now returns an error saying I don't have permission to use the API. This of course doesn't tell you much, so let me get into some details about the apps.

They are drupal 8 apps, served by docker (and docker-compose). I have checked that they have access to the internet (more about this later), and the app's connection to a local mysql server is working fine. I have tried to restart docker, rebuilding the containers, and such standard hacks, but nothing seems to fix it. I have a laravel app served locally with docker aswell, using the same API, and this app can retrieve and post data from the API, so the problem seems to have to do with the drupal code. Althought this might not be true, since I have tried to use older versions before this started happening, and the same error still occurs. This leads me to think that it could also be an ssl certificate error, but I have not found a way to test this.

Some strange things I have noticed:

  • The production version of the drupal app (with identical code) served on a remote server we control is working, and it is using the same API (Ubuntu is run both locally and on the remote server).
  • When I turn on my computer, the the apps (both laravel and druppal) does not have internet, and I have to stop and start (with docker-compose down and up) them for them to get internet.

I know this is not at all enough to find out exactly what is wrong here, but I am hoping that someone might have seen some of these problems before, and give a hint to fix something.

As always, if you need any specific code or explanation of the app , just ask, and I'll happily supply it.

Update: I forgot to add that the app is served with this proxy, which has been acting up some. When I run it, I get the application "letsencrypt_ser" running on my machine, not in the docker container, and it takes more than 100% of CPU (4 cores, so more than one core's worth). This application is not run on the remote server running the same setup, so I believe something is horribly wrong with my container.

Also, in the docker-compose file for my drupal add, we have the code

networks:
  default:
    external:
      name: webproxy

in the end, to connect the container to the proxy.

like image 517
Richard Jensen Avatar asked Nov 22 '25 17:11

Richard Jensen


1 Answers

You should use \Drupal::httpClient(); instead of curl in drupal 8 (but curl should work too).

What's in your docker-compose.yml ?

Do you have something like this for your services ?

    services:
      api:
        extra_hosts:
          - "my_api_hostname.local:0.0.0.0"
        networks:
          defaults:
            aliases:
              - my_api_hostname.local
      
      front:
        depends_on:
          - api
        extra_hosts:
          - "my_front_hostname.local:0.0.0.0"
        networks:
          defaults:
            aliases:
              - my_front_hostname.local
like image 106
MacSim Avatar answered Nov 24 '25 07:11

MacSim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!