This is one of my favorite passages of Scripture. It has so many good lessons that we could learn. I am sure you have heard the story of Joseph, how he was sold into slavery, then thrown into prison. There are a couple of thoughts that I have, these are very important for you to understand if you are to receive all of the Lord's blessings He has for you.
In life you can do everything right, follow God, live a Godly life, and still end up in "Life's prison". Joseph may have been a little cocky in his young age, but surely did not deserve to have his brothers turn on him and be sold to be a slave. He also could have easily slept with Potiphar's wife, but he chose to do the Godly thing and run, he ended up in prison.
Life is what you make of it. Joseph, if anyone, had every right to be angry and bitter towards God. After all, he did the right thing, and this is what "Doing the right thing" landed him; in prison. Joseph instead accepted where God had him. I can imagine it is not where Joseph thought he would have been, but he knew God was in control.
Wherever you are in your life, whatever your situation, GOD IS WITH YOU. When Joseph was a slave, GOD WAS WITH HIM (v39:2) When Joseph was in prison GOD WAS WITH HIM (v39:21,23) Let me say this again, wherever you are, GOD IS WITH YOU, and if God is with you, you are blessed, all you need to do is accept His blessings.
Genesis 39:2, 20-23
"The LORD was with Joseph and he prospered, and he lived in the house of his Egyptian master.
Joseph's master took him and put him in prison, the place where the king's prisoners were confined. But while Joseph was there in the prison, the LORD was with him; he showed him kindness and granted him favor in the eyes of the prison warden. So the warden put Joseph in charge of all those held in the prison, and he was made responsible for all that was done there. The warden paid no attention to anything under Joseph's care, because the LORD was with Joseph and gave him success in whatever he did."