Fitness is a product of duration and intensity. You can do it a zero speed and zero distance on a trainer, moderate speed and distance on an MTB, or greater speed and distance on a road bike. Use whichever one or combination works for you with respect to convenience, time, opportunity, and motivation.
Times and intensities are usually schedulized to promote optimal training response for the intended purpose or goals, AKA training plans.