The ancient Hindu text the Bhagavad Gita contains several passages describing how to be detached from various inner states, pleasure and pain, and the senses so as to observe them and transcend them and find inner peace.
Here are three passages that are particularly instructive into how to do it:
“From the world of the senses, Arjuna, comes heat and comes cold, and pleasure and pain. They come and they go: they are transient. Arise above them, strong soul.
The man whom these cannot move, whose soul is one, beyond pleasure and pain, is worthy of life in Eternity.”
~ Juan Mascaro (translator), The Bhagavad Gita, (The Penguin Group: first published 1962, reprinted 2003). Chapter 2, Verses 14-15.
“Think thou also of thy duty and do not waver. There is no greater good for a warrior than to fight in a righteous war.
There is a war that opens the doors of heaven, Arjuna! Happy the warriors whose fate is to fight such war.
But to forgo this fight for righteousness is to forgo thy duty and honour: is to fall into transgressions.[…]
Prepare for war with peace in thy soul. Be in peace in pleasure and pain, in gain and in loss, in victory or in the loss of a battle. In this peace there is no sin.”
~ Juan Mascaro (translator), The Bhagavad Gita, (The Penguin Group: first published 1962, reprinted 2003). Chapter 2, Verses 31-33, 38.
“…When pleasure comes he is not shaken, and when pain comes he trembles not.
He is not bound by things without, and within he finds inner gladness. His soul is one in Brahman and he attains everlasting joy.
For the pleasures that come from the world bear in them sorrows to come. They come and they go, they are transient: not in them do the wise find joy.
But he who on this earth, before his departure, can endure the storms of desire and wrath, this man is a Yogi, this man has joy.
He has inner joy, he has inner gladness, and he has found inner light. This Yogi attains the Nirvana of Brahman: he is one with God and goes unto God.”
~ Juan Mascaro (translator), The Bhagavad Gita, (The Penguin Group: first published 1962, reprinted 2003). Chapter 5, Verses 20-24.