The distance restriction for building walls applies to the surface only. For the caverns you can build directly at the edges, so it's possible to secure them completely. I generally leave 4 "pockets" (one at each side) where entering critters can be kept outside, unless they're interesting, in which case I lower a drawbridge to let them into the airlock (and the cages there). If you do get an issue with reanimated critters the airlock can be modified for capturing/destruction of critters. Note that if you get undead entering, they typically cycle through as normal visiting critter groups, and thus leave after a while, so unless a pocket has both living and undead critters entering there should be no buildup, although FBs can contribute to a bit of a mess to handle.
Fighting is dangerous, so running away is typically a safer option than embarking with a fighting squad. Dig out an area as quickly as possible and block access with a door or hatch, haul as much of your stuff inside, and keep an eye open for dangerous critters entering (the units-other tab is useful for that). Getting farming going inside in case of you having to turtle is important, as well as getting building material inside for building of workshops, in particular if you have an aquifer blocking access to stone (in which case it's important to also ensure you get the parts for a screw pump so you can pierce the aquifer while waiting out a siege).
A single dorf fortress will have to do what's needed to keep things going and prepare for migrants. Also note that while a door/hatch is an initial line of defense, an airlock (two drawbridges with some room in between) is where you'd want to go unless playing with an "open fortress" restriction. Note that a locked door may give you enough time to build a wall inside of it should you encounter building destroying invaders.