Atomic chains are perfect systems for getting fundamental insights into the electron dynamics and coupling between the electronic and ionic degrees of freedom in one-dimensional metals. Depending on the band filling, they can exhibit Peierls instabilities (or charge density waves), where equally spaced chain of atoms with partially filled band is inherently unstable, exhibiting spontaneous distortion of the lattice that further leads to metal-insulator transition in the system. Here, using high-resolution scanning transmission electron microscopy, we directly image the atomic structures of a chain of iodine atoms confined inside carbon nanotubes. In addition to long equidistant chains, the ones consisting of iodine dimers and trimers were also observed, as well as transitions between them. First-principles calculations reproduce the experimentally observed bond lengths and lattice constants, showing that the ionic movement is largely unconstrained in the longitudinal direction, while naturally confined by the nanotube in the lateral directions. Moreover, the trimerized chain bears the hallmarks of a charge density wave. The transition is driven by changes in the charge transfer between the chain and the nanotube and is enabled by the charge compensation and additional screening provided by the nanotube.