A variational framework for the identification and analysis of general nonlinear optimal disturbances in compressible flows is derived. The formulation is based on the compressible Navier–Stokes equations in conserved variables for an ideal gas with temperature-dependent viscosity. A discretely consistent implementation based on generalized coordinates allows the accurate analysis of a wide range of settings. An application in the identification of the optimal disturbances which experience the highest amplification in kinetic energy in pipe flow is presented. At low Mach numbers and moderate initial amplitude, the disturbances undergo a sequence of Orr mechanism, oblique nonlinear interaction and lift-up mechanism, and the energy amplification is consistent with results reported for incompressible flow (Pringle & Kerswell, Phys. Rev. Lett., vol. 105, 2010, 154502). When the Mach number is increased, the gain in perturbation kinetic energy grows appreciably, and the initial disturbance field becomes increasingly localized. Nonlinear optimal disturbances which are rescaled to higher initial kinetic energy than prescribed in the optimization procedure are demonstrated to evolve into a chaotic state. For a constant time horizon, the initial perturbation energy to reach a high-energy state decreases monotonically with Mach number.