If the city was specifically built for that purpose or had a few years to prepare its infrastructure for self-sufficiency, that might include setting up a fortress-style walled city, and beds for super-efficient gardening (including choosing only crops that are very well suited for this but also resilient and stable), moving a big part of the living quarters underground so that buildings would not take sunlight away from crops, and choosing carefully how many people (mouths to feed/hands to work) and what kinds of people (genetic diversity, skills/education, social stability) to take in. I see no reason why all that could not be achieved in less than five years, if it was someone's priority, and there are multiple plausible reasons why it could be. The only way a city like that, given sufficiently strong walls, could be overcome is by the besiegers manipulating the wheather or blocking out the sun.