Confinement of dislocations inside a crystal with a prescribed external strain