Natural protein assemblies have many sophisticated architectures and functions, creating nanoscale storage containers, motors and pumps. Inspired by these systems, protein monomers have been engineered to self-assemble into supramolecular architectures including symmetrical, metal-templated and cage-like structures. The complexity of protein machines, however, has made it difficult to create assemblies with both defined structures and controllable functions. Here we report protein assemblies that have been engineered to function as light-controlled nanocontainers. We show that an adenosine-5-triphosphate-driven group II chaperonin, which resembles a barrel with a built-in lid, can be reprogrammed to open and close on illumination with different wavelengths of light. By engineering photoswitchable azobenzene-based molecules into the structure, light-triggered changes in interatomic distances in the azobenzene moiety are able to drive large-scale conformational changes of the protein assembly. The different states of the assembly can be visualized with single-particle cryo-electron microscopy, and the nanocages can be used to capture and release non-native cargos. Similar strategies that switch atomic distances with light could be used to build other controllable nanoscale machines.