I'm trying to get the client IP in settings.php for security reasons, using the following code.
I have to support X-Forwarded-For
header because accessing web server over loadbalancer.
$clientIp = \Drupal::request()->getClientIp();
It causes the following error.
The website encountered an unexpected error. Please try again later.
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 129 of /var/www/my-site/docroot/core/lib/Drupal.php).
Drupal::getContainer() (Line: 218)
Drupal::request() (Line: 759)
require('/var/www/my-site/docroot/sites/default/settings.php') (Line: 116) Drupal\Core\Site\Settings::initialize('/var/www/my-site/docroot', 'sites/default', Object) (Line: 1015)
Drupal\Core\DrupalKernel->initializeSettings(Object) (Line: 642)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Any suggestions to fix this?