Search code examples
phpazuredockeryii2

How to resolve: Not instantiable – yii\di\NotInstantiableException in azure?


I have dockerized an basic template yii2 app and I can run the docker container without any problems on localhost.

But I pushed the repository to azure. And I hit the url:

http://suitecontainer.h0gkfpg3e6gddyhe.germanywestcentral.azurecontainer.io

But then I get this error:

Not instantiable – yii\di\NotInstantiableException
Failed to instantiate component or class "yii\debug\Module".
↵
Caused by: ReflectionException
Class "yii\debug\Module" does not exist
in /app/vendor/yiisoft/yii2/di/Container.php at line 507

Dockerfile looks:

FROM yiisoftware/yii2-php:8.2-apache

RUN a2enmod rewrite

WORKDIR /app

COPY . /app

ADD composer.lock composer.json /app/
RUN composer install --prefer-dist --optimize-autoloader --no-dev && \
    composer clear-cache


RUN mkdir -p runtime web/assets && \
    chmod -R 775 runtime web/assets && \
    chown -R www-data:www-data runtime web/assets

RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

RUN service apache2 restart

docker-compose:

version: "3.8"
services:
  php:
    image: crisuite20backendtest.azurecr.io/web1
    container_name: yii2
    build:
      context: ./
      dockerfile: Dockerfile
    restart: always
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
   
    ports:
      - "8000:80"

    networks:
      - yii
networks:
  yii:

And I did a composer upate. require-dev from composer.json looks:

    "require-dev": {
        "yiisoft/yii2-debug": "~2.1.0",
        "yiisoft/yii2-gii": "~2.2.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/codeception": "^5.0.0 || ^4.0",
        "codeception/lib-innerbrowser": "^4.0 || ^3.0 || ^1.1",
        "codeception/module-asserts": "^3.0 || ^1.1",
        "codeception/module-yii2": "^1.1",
        "codeception/module-filesystem": "^3.0 || ^2.0 || ^1.1",
        "codeception/verify": "^3.0 || ^2.2",
        "symfony/browser-kit": "^6.0 || >=2.7 <=4.2.4"
    },

index.php looks:

<?php

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../config/web.php';

(new yii\web\Application($config))->run();

Question: how to resolve the error: Failed to instantiate component or class "yii\debug\Module". from Azure?


Solution

  • Class yii\debug\Module is part of yiisoft/yii2-debug package. That package is only required in your composer.json as "dev" requirement. But in your Dockerfile you are running composer install with --no-dev option. Meaning that "dev" requirements are not being installed.

    The app is then trying to load module from package that is not available.

    By default Yii2 app only tries to load yii\debug\Module when it's running in debug mode. So this error usually means that you've installed packages for production (with --no-dev option) but left your app in debug mode.

    To switch app from debug mode to production mode you have to remove (or comment out) following lines in web/index.php:

    // comment out the following two lines when deployed to production
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    

    Or, if you need to run app in debug mode in Azure remove --no-dev option in this line of your Dockerfile:

    RUN composer install --prefer-dist --optimize-autoloader --no-dev && \
        composer clear-cache