Hello Camembert, thank you for posting your comment and sorry that you are having this trouble and perhaps you could ask your doctor/psychiatrist if you could take your medication at night time, and that's only a suggestion as I'm not qualified to say.
It must seem to be a catch-22 because the medication is working but you're having nightmares, however, these may still be caused by your original condition and why you needed to begin taking this medication.
Sometimes by having CBT you are able to figure out why they do occur or how they would like to take place.
I also have nightmares and have had them for years, now when I wake up I can see the funny side to them knowing that they aren't going to happen and most times people from years ago appear, people who I've had no contact with for years, that's why I know they aren't true.
Try and rationalise what you have dreamt and consider if they could possibly be true in the situation you're in and realise they that this can't physically be done.