Nextcloud 17.0.3 can't be reached from another docker container

I’m using docker-compose to run a few containers. On of these containers is my app that uses nextcloud that is also containerized and I’ve noticed a problem with trusted domains.

My docker-compose file

version: '3'
services:
  nextcloud:
    container_name: "nextcloud"
    image: nextcloud:17.0.3
    ports:
      - "8888:80"
    volumes:
      - "nextcloudVolume:/var/www/html"
….
 my-app:
    container_name: "my-app"
    domainname: "my-app"
    image: my-app
    ports:
      - "8081:8081"
    depends_on:
      - "nextcloud"

I have no problem using nextcloud in my browser on localhost:8888. I also can reach nextcloud from my non-containerized app. But when my app is containerized and is trying to reach nextcloud I get 400 error

172.18.0.8 - - [19/Feb/2020:12:50:46 +0000] "PUT /remote.php/dav/files/user/dummy_message.json HTTP/1.1" 400 5488 "-" "Sardine/5.9"

After some investigation I’ve found that my domain has to be trusted. To do this I’ve used the following command

docker exec --user www-data -it a38daf9b273e /bin/bash

www-data@a38daf9b273e:~/html$ php occ config:system:get trusted_domains
localhost:8888
www-data@a38daf9b273e:~/html$ php occ config:system:set trusted_domains 1 --value 172.18.0.8
System config value trusted_domains => 1 set to string 172.18.0.8
www-data@a38daf9b273e:~/html$ php occ config:system:get trusted_domains
localhost:8888
172.18.0.8
www-data@a38daf9b273e:~/html$ cat config/config.php 
<?php
$CONFIG = array (
  'htaccess.RewriteBase' => '/',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'apps_paths' => 
  array (
    0 => 
    array (
      'path' => '/var/www/html/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 => 
    array (
      'path' => '/var/www/html/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'instanceid' => 'oczx51s2p4gb',
  'passwordsalt' => 'OYqcRzg/5qxjZmcFP4PrdoK8fvvKW+',
  'secret' => 'LAxLfXR0UvxpznJik3qTYl9bTWkBplzwrNisJJaQCm8EydKA',
  'trusted_domains' => 
  array (
    0 => 'localhost:8888',
    1 => '172.18.0.8',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'sqlite3',
  'version' => '17.0.3.1',
  'overwrite.cli.url' => 'http://localhost:8888',
  'installed' => true,
);

This didn’t work, so I’ve tried to add my host as well (that is my-app container name)

www-data@a38daf9b273e:~/html$ php occ config:system:set trusted_domains 2 --value my-app
System config value trusted_domains => 2 set to string my-app
www-data@a38daf9b273e:~/html$ php occ config:system:get trusted_domains
localhost:8888
172.18.0.8
my-app

This didn’t help. I’ve tried to use environment variable to fix this issue like this

    environment:
      - NEXTCLOUD_TRUSTED_DOMAINS=my-app localhost

But no luck as well. I still get

172.18.0.8 - - [19/Feb/2020:12:58:21 +0000] "PUT /remote.php/dav/files/user/dummy_message.json HTTP/1.1" 400 5496 "-" "Sardine/5.9"

The url that I’m using in my-app is

http://nextcloud:80/remote.php/dav/files/user/dummy_message.json

With the following body

{
  "author": "user1",
  "message": "Hello NextCloud"
}

According to online discussions this solution should be right, but for some reason it doesn’t work for me. Does anyone have a clue what I’m doing wrong?

My environment:
Nextcloud version 17.0.3
Operating system and version macOs Catalina 10.15.3
Docker version 19.03.5, build 633a0ea
Docker compose version 1.25.4, build 8d51620a

Please let me know if you need extra info please and thanks in advance

I’ve managed to bypass this problem with the following workaround

php occ config:system:set trusted_domains 1 --value *:*

Since I’m using it for testing needs it’s fine for me, but overall it doesn’t seem right

did you try nextcloud as trusted domain?

this is the ip of the nextcloud container?

  1. Nope, I didn’t try nextcloud as trusted domain, but don’t really understand why this should work. In this context nextcloud is nextcloud containter name and in docker if 2 containers are in the same network it’s possible to call service in container by its container name. So I can call nextcloud with http://nextclodu:80 and my-app with http://my-app:80. As far as I understand adding nextcloud to trusted list will basically mean that I trust localhost

  2. No, 172.18.0.8 is my-app container ip. For nextcloud this is client ip that I was trying to make trusted

I know this is quite old post, but I am struggling with the exact same issue you were having. Did you ever find a solution?