If there are other methods short of prison that can render law-breakers harmless - such as restrictions on certain activities and occupations - then those should be pursued first.
The ghost of this philosophy, however attenuated, can be seen in systems of pardon and parole.
I acknowledge that a desire for retribution - to punish the evil-doer; make them suffer for what they've done - is a strong impulse (I feel it myself!), deeply imbedded in our tribal psyches, but it should be fought, not indulged.
This seems to me to be the only moral basis for a system of justice and incarceration, though I have no idea how to nudge a society towards this model. Some northern European countries approach it.