According to the non-profit project Coral Science, coral reefs are not lodged in soil. The rocky substance in which they are fixed is made by the corals themselves. They live in colonies made of many polyps that build skeletons from the underside of their skin.
Corals are tiny animals belonging to the group called Cnidaria, which includes jellyfish, sea anemones and hydras. The hard calcium carbonate skeleton they produce connects them together and provides protection from predators. The polyps can contract inside the hard skeletal structure when a predator approaches. They continue to deposit calcium carbonate on these structures, which allows the formation of coral reefs over time.