Most people who get addicted to drugs are using them to escape pain. This is occasionally physical but usually mental. It is memories of traumatic experiences or abuse, loss, stress, etc.
That is why incarceration is so counterproductive, you are just giving the addict more pain they need to escape. Also treating drug addiction itself without understanding and addressing the underlying reason for it will just lead to constant relapsing.
I recommend the book Chasing the Scream for people who want a better understanding of drug addiction.