We study the thermodynamic behavior of static and spherically symmetric hairy black holes in massive gravity. In this case, the black hole is surrounded in a spherical cavity with a fixed temperature on the surface. It is observed that these black holes have a phase transition similar to the liquid-gas phase transition of a Van der Waals fluid. Also, by treating the cosmological constant $\Lambda$ as a thermodynamic pressure $P$, we study the thermodynamic behavior of charged anti-de Sitter black holes in an ensemble with a pressure of $P$ and an electric potential $\Phi$ as the natural variables. A second order phase transition is observed to take place for all the values of the electric potential $\Phi$.