You can modify the iteration, for example by including a projection. That's because if $x\in D$ is indeed a fixed point of the function, i.e. $x=f(x)$, then it is also a fixed point of $x=\Pi(f(x))$ where $\Pi$ is some kind of operation so that
$$
\Pi(x) = \begin{cases} x & \text{if $x\in D$} \\ \text{some $y\in D$} & \text{otherwise}. \end{cases}
$$
An example is the orthogonal projection onto $D$ if $D$ is a convex set. But anything else that satisfies the statement above is valid as well.
So, with this, your iteration then becomes $x_{n+1}=\Pi(f(x_n))$ and that avoids the problem of walking out of the domain.
The question you need to answer, however, is whether the combined operation $\Pi\circ f$ is still a contraction. If your fixed point lies in the interior of $D$, then if $f$ is a contraction at $x$, then $\Pi\circ f$ is clearly also a contraction at $x$. But it may not be far away from the fixed point, and it's not obvious that the iteration will converge.