A hierarchically porous ZIF-8 (HZIF-8) with pH-responsive and antibacterial activity was constructed using bicomponent hydrogel as template. The synthesized HZIF-8 was applied to load the antibacterial drug berberine hydrochloride (BBH). In vitro release studies showed that BBH@ZIF-8 and BBH@HZIF-8 displayed a sustained drug release profile, and the drug release rate increased with the decreasing pH value. The BBH@HZIFS with the highest drug loading capacity also had the highest cumulative drug release rate. At the end of 24 h, the cumulative release of BBH encapsulated in HZIFS, HZIFM and HZIFP were 92.07%, 86.60% and 88.00% at pH 5.0, respectively, higher than that of ZIF-8 (84.07%). Furthermore, in vitro antibacterial testing revealed that BBH@HZIF-8 exhibited efficient and long-lasting effects against Staphylococcus aureus, which was attributed to the synergistic bacteriostatic effect of Zn2+ and BBH released by the structural degradation of BBH@HZIF-8 under acidic conditions. In conclusion, the study provides a strategy for synthesizing HZIF-8 and demonstrates the potential of HZIF-8 as a pH-responsive nanoplatforms for antibacterial applications.