That can be done, but requires good pre-planning. There are websites available for schedules of ferries to and from Sorrento/Capri and Naples. I would suggest taking the train to Pompeii, spend a couple of hours there and then continue on the train to Sorrento see it, grab a ferry to Capri and then go from Capri directly back to Naples.
Leave buffer time at the end and buy your return to Naples ferry tickets as soon as you get to Capri so that you know you have a seat and will get back. It will be rushed but its doable provided you get off the ship ASAP in the morning and the ship doesn't leave until the evening.
Carnival Cruise Lines:
Fantasy 90, Celebration 95, Victory 05, Conquest 05, Spirit 6/06, Freedom 6/08, Holiday 7/08, Victory 6/09, Valor 9/09, Dream 6/10, Splendor 8/10,Conquest 8/11,Magic 11/11,Triumph 4/12,Spirit 7/12,Freedom 11/12, Triumph 2/13 - The fire cruise
NCL Dream October 2006, RCCL Mariner OTS June 2007, Celebrity Silhouette 2013