As Hailong said in his comment this only happens in the Gorenstein case; here is a sketch of an argument.

Suppose $X$ is a quasi-compact quasi-separated scheme with a dualising complex $D$ and let us assume $D$ is perfect. Recall that $\mathsf{D}^\mathrm{perf}(X)$, the category of perfect complexes over $X$, is a rigid tensor category i.e., it is closed symmetric monoidal and setting, for E a perfect complex, $E^\vee = hom(E,\mathcal{O}_X)$ we have natural isomorphisms
$$ E^\vee \otimes F \cong hom(E,F)$$
for all perfect complexes F (here I am being notationally lazy - of course the internal hom and tensor are the derived ones).

Now we just notice that there are isomorphisms
$$R \cong hom(D,D) \cong D^\vee \otimes D$$
so $D$ is an invertible object with respect to the tensor product in $\mathsf{D}^\mathrm{perf}(X)$. This implies $D$ is isomorphic to a shift of a line bundle on each connected component of $X$ (see for instance Prop 6.4 of "Gluing techniques in triangular geometry" by Balmer and Favi for a short proof, although, I should mention, the result is pretty well known and older than that paper). Thus $X$ is Gorenstein.