The best practice is to delegate this task to a service.
See this example: https://symfony.com/doc/current/console.html#getting-services-from-the-service-container
However you can also add a constructor to the command and give it a ContainerInterface. Then you just do $this->container->get('doctrine')->getManager();
// YourCommand.php
private $container;
public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->container = $container;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->container->get('doctrine')->getManager();
// do stuff...
}
Also, don't forget to add the proper "use" statement at the beggining of your script:
use Symfony\Component\DependencyInjection\ContainerInterface;