How to use my iMac 2017 as a second monitor for my MacBook Pro mid 2012?
I don't think there's a built-in way to accomplish what you're going for. But there are some products (hardware and/or software) you can purchase to help.

iMacs used to have a mode wherein they could act as a secondary external display. Even though current iMacs no longer offer that functionality, this product provides functionality to turn a Mac into a secondary display. I've heard very good things about it, but I've never tried it myself. It requires a hardware dongle plugged into the source Mac (where the video is coming from):
https://lunadisplay.com/pages/meet-mac-to-mac-mode

And here's another that advertises similar functionality. Unlike Luna Display above, it doesn't require a hardware dongle:
https://www.duetdisplay.com/air

(I don't have any relationship with either company or product.)