Electric dog fences are really only safe for dogs that don't really need fencing. An excited dog who sees another dog, a squirrel, a cat, is liable to run right through the electric fence, and will then be stuck outside it unable to get home again. They can be very dangerous for this reason. They also don't protect your dog from anything that might come onto your property - so if your dog is out there on his own, another dog might come in and attack him.
If your dog is capable of learning not to run away from home, you can teach him without shocking him with electricity to do that, as long as you aren't going to leave him out there alone for hours.
Give him lots of exercise, lots of social contact, lots of training and in between all that, plenty of company and quiet downtime, and there's a decent chance he will not want to leave your home. Sit outside with him and practice. Give him a command to leave your property, and reward him for waiting on the boundary. Here's a good video showing how to do this : https://www.youtube.com/watch?v=OuTh47i3hOY
If your dog is young and silly, or an enthusiastic chaser, and there are Way Too Fun things out there for him to run after, do not rely on an electric fence. It will not be safe. Use a leash and lots of supervision.
There is no invisible solution that will protect a dog being left outside unsupervised for long periods, and it would really be best to find a different management approach if that is your problem.