After this, the king of Egypt's cupbearer and his baker offended their master, the king of Egypt.
References for Genesis 40:1
Pharaoh was angry with his two officers, the chief cupbearer and the chief baker,
and put them in custody in the house of the captain of the guard, in the prison where Joseph was confined.
The captain of the guard assigned Joseph to them, and he became their personal attendant. And they were in custody for some time.
References for Genesis 40:4
The cupbearer and the baker of the king of Egypt, who were confined in the prison, each had a dream. Both had a dream on the same night, and each dream had its own meaning.
When Joseph came to them in the morning, he saw that they looked distraught.