Considering a canonical ensemble, in which the temperature and the charge on a wall of the cavity are fixed, we investigate the thermodynamics of a D-dimensional Gauss-Bonnet black hole in a finite spherical cavity. Moreover, it shows that the first law of thermodynamics is still satisfied. We then discuss the phase structure and transition in both five and six dimensions. Specifically, we show that there always exist two regions in the parameter space. In one region, the system possesses one single phase. However in the other region, there could coexist three phases and a van der Waals-like phase transition occurs. Finally, we find that there is a fairly close resemblance in thermodynamic properties and phase structure of a Gauss-Bonnet-Maxwell black hole, either in a cavity or in anti-de Sitter space.