"Rejoice in the Lord always. I will say it again: rejoice!" Philippians 4:4
Fill Your Wandering Heart with Thankfulness (from DesiringGod.org)
I highly commend this article from Desiring God. I'd add that the principle extends virtually to anything with which you may be discontent, from other sins to relationship problems to mental illness to lack of contentment itself. Gratefulness and thanksgiving can cure a great many ills, and those they do not cure they will empower you to live with in a healthier way.
Regarding sin, I believe all sins are ultimately special cases of covetousness. John Piper has defined covetousness in this way: "Coveting is desiring anything other than God in a way that betrays a loss of contentment and satisfaction in him." We are to have no other gods before God (Exodus 20:3). When I delight in the trappings of pursuing other gods (e.g., anger or bitterness in pursuit of self-righteousness, sexual pleasure outside of biblical covenant marriage, professional success for love of money, etc.), I am ultimately serving a false god (Colossians 3:5). Specific sins are sinful because I have substituted the worship of (delight in) created things for worship of (delight in) the Creator (Romans 1:25).
The simple (though admittedly not easy) antidote to covetousness is gratefulness and thanksgiving. This is more than just a theological or philosophical point. It is a plug-and-play, ready-made one-to-one replacement behavior. An important principle in behavior change is that you cannot NOT do something. When you are refraining from a behavior, you are behaving in some other way. So if there is a behavior you want (or need) to replace, the most effective way to do so is to engage in a behavior that is incompatible with the unwanted behavior. Ideally this behavior would also move you into greater alignment with those deep-down values that you ultimately want your life to be about.
A simple example is nail biting. I cannot simply not bite my nails; any time I am not biting my nails, I am doing something else with my hands. If I want to eliminate nail biting, the goal is to replace that behavior by doing something else with my hands, something incompatible with nail biting. For example, when I catch myself biting or having the urge to bite, I can sit on my hands, as sitting on my hands is incompatible with biting my nails. Digging deeper, if I recognize that I bite my nails when I'm stressed, I can replace nail biting with massaging or stretching my hands. This activity is also incompatible with nail biting, but it also moves me more in line with the deeper need for calm.
Applying this spiritually, we must begin with embracing this truth: "[God's] divine power has given us everything required for life and godliness..." (2 Peter 1:3) Note the he has given us everything "required," not necessarily "desired." We must be really clear on the distinction between needs and wants, and we need to let God's Word define those for us.
Applying this practically, 2 Peter 1:3 is an ideal prayer and meditation for replacing any covetous discontentment. When I find myself pulled toward ungodly passions, I'll often begin by praying, "Thank you, Lord, that you have given me everything I need for life and godliness." This quickly and efficiently focuses my attention on God, his power, and his purposes. From there, I may give thanks for provisions specific to the temptation. For example, if the temptation is toward lust, I can thank God that he has provided sexual pleasure to be enjoyed in marriage (1 Corinthians 7:1-5). If I am married, I can express thanksgiving for my spouse; if I'm not, I can thank God that he has given me everything required for life and godliness, and that if he hasn't called me to be married then I don't require sexual pleasure for life or godliness.
The same principle applies for other temptations. If my anger tempts me to sin, I can reflect on the truth that vengeance belongs to our sovereign God (Romans 12:19). If I'm tempted toward overeating I can reflect on the command to make no plan to gratify the desires of the flesh (Romans 13:14) and again be thankful that God gives everything I need for life and godliness. Tempted to "keep up with the Joneses" (car, house, stuff)? Same idea.
Ultimately, giving thanks is a command we must obey. But far from a burden, obeying this command clears the path for the Lord to satisfy all of our needs with the ultimate source and object of all our desires: himself.