Recent studies on the interplay between band topology and topological defects in real space offer an unprecedented opportunity to design photonic cavities. Here, we propose a concept of gradient dislocation by placing two chunks of square-lattice photonic crystals with the same width but differing by one period together, which is described by a one-dimensional Dirac equation with a position-dependent and sign-switching mass distribution. A photonic bound state, dubbed the Dirac cavity mode, localized at the center of the gradient dislocation is demonstrated. Compared to est modal area and a relatively high Q-factor, with its frequency demonstrating robustness against certain perturbations. We also discuss a potential application in index sensing by designing a coupled cavity-waveguide system based on the photonic Dirac cavity. Our work presents an ultracompact photonic cavity design and paves the way toward future photonic integrated circuits.