these are just suggestions: feed him earlier in the day; walk him or let him out closer to bedtime and more often. Crate him at bedtime. If he has loose stool try giving him sweet potato or canned pumpkin , it firms up the stool. DO NOT punish him he has no idea why you are upset with him, . If the correction doesn't come within seconds of the unwanted behavior it is meaningless to him. Even if he "looks guilty" he is just responding to your behavior. hope this helps.
Is this something new or has he been do-ing this all along? Perhaps your dog is upset by some change in the household. Think about why. Is the dog getting some individual play and pat time with a human at your house on at least a daily basis? Also, very much agree with the long walk idea or going out with the dog for a little outside fresh air time assuring your dog takes a crap.
Get a kennel crate and keep him in it at night. Most digs wont poop where they sleep. Take him outside right before bed and praise him if he poops outside. Then take him out of the crate and outside early in the morning again. Do this every day. If you catch him pooping in the house scold him tgem take him out right away.
I had the same problem with an older dog that I had as well. What I found very helpful was waking up in the night and bringing the dog on a walk so that he could use the restroom. Or you can train your dog to jingle a bell on the door handle when he needs to go outside, our puppy knows how to do this and it is very helpful.