title: Calling service from Symfony Command fails with "The container cannot be retrieved as the application instance is not yet set." error, why?
body: <p>I have the following services definition at <code>IntegrationBundle/Resources/config/services.yml</code>:</p>
  
  <pre><code>services:
    event.subscriber.params.extractor:
      class: IntegrationBundle\Middleware\EventSuscriberParamsExtractor
  
    main.api:
      class: IntegrationBundle\API\MainAPI
      calls:
        - [setContainer, ['@service_container']]
    order.push.api:
      class: IntegrationBundle\API\OrderPushAPI
      calls:
        - [setContainer, ['@service_container']]
  
    process.account.api:
      class: IntegrationBundle\API\ProcessAccountData
      arguments:
        - '@event.subscriber.params.extractor'
      calls:
        - [setContainer, ['@service_container']]
    ...
  </code></pre>
  
  <p>I need to access <code>main.api</code> from within a Symfony Command and this is how I am doing it:</p>
  
  <pre><code>namespace IntegrationBundle\Command;
  
  use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  use Symfony\Component\Console\Command\LockableTrait;
  use Symfony\Component\Console\Input\InputArgument;
  use Symfony\Component\Console\Input\InputDefinition;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Input\InputOption;
  use Symfony\Component\Console\Output\OutputInterface;
  
  class SchneiderAPICommand extends ContainerAwareCommand
  {
      use LockableTrait;
  
      protected function configure()
      {
          $this
              -&gt;setName('api:execute')
              -&gt;setDefinition(
                  new InputDefinition([
                      new InputOption('source', '', InputArgument::OPTIONAL, '', 'Wonderware'),
                      new InputOption('object', '', InputArgument::OPTIONAL, '', 'Account,AgreementHistory'),
                      new InputOption('quantity', '', InputArgument::OPTIONAL, '', $this-&gt;getContainer()-&gt;getParameter('api_items_to_process')
                      ),
                  ])
              );
      }
  
      protected function execute(InputInterface $input, OutputInterface $output)
      {
          // Prevent multiple executions of a console command
          if (!$this-&gt;lock()) {
              $output-&gt;writeln('The command is already running in another process.');
  
              return 0;
          }
  
          $output-&gt;writeln($this-&gt;getContainer()-&gt;get('main.api')-&gt;execute(
              $input-&gt;getOption('source'),
              str_replace(',', '|', $input-&gt;getOption('object')),
              $input-&gt;getOption('quantity')
          ));
      }
  }
  </code></pre>
  
  <p>Then I am trying the command as follow:</p>
  
  <pre><code>$ bin/console api:execute --source=Wonderware --object=Account --quantity=50 -vvv  
  
    [LogicException]                                                               
    The container cannot be retrieved as the application instance is not yet set.  
  </code></pre>
  
  <p>As you can see is failing and I am not sure what I am missing here. Below is a stack trace (if that help somehow):                                                                     </p>
  
  <pre><code>Exception trace:
   () at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php:40
   Symfony\Bundle\FrameworkBundle\Command\ContSchneiderainerAwareCommand-&gt;getContainer() at /var/www/html/symfony/src/IntegrationBundle/Command/MainAPICommand.php:38
   IntegrationBundle\Command\MainAPICommand-&gt;configure() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:63
   Symfony\Component\Console\Command\Command-&gt;__construct() at n/a:n/a
   ReflectionClass-&gt;newInstance() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Bundle/Bundle.php:193
   Symfony\Component\HttpKernel\Bundle\Bundle-&gt;registerCommands() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:135
   Symfony\Bundle\FrameworkBundle\Console\Application-&gt;registerCommands() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:108
   Symfony\Bundle\FrameworkBundle\Console\Application-&gt;all() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:72
   Symfony\Bundle\FrameworkBundle\Console\Application-&gt;doRun() at /var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:124
   Symfony\Component\Console\Application-&gt;run() at /var/www/html/symfony/bin/console:28
  </code></pre>
  
  <p>I have been reading <a href="https://php-and-symfony.matthiasnoback.nl/2013/10/symfony2-console-commands-as-services-why/" rel="nofollow noreferrer">here</a> (I do not want and do not need to define this command as a service), <a href="http://symfony.com/doc/current/console.html" rel="nofollow noreferrer">here</a>, <a href="https://coderwall.com/p/gtk3tw/symfony2-call-service-from-command" rel="nofollow noreferrer">here</a> and <a href="https://github.com/symfony/framework-bundle/blob/master/Command/ContainerAwareCommand.php" rel="nofollow noreferrer">here</a> but none of them help me.</p>
  
  <p>Can any give me some help here? What I am missing?</p>