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 Λ 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 Φ as the natural variables. A second order phase transition is observed to take place for all the values of the electric potential Φ.