Docker is 1) namespace and chroot for separating processes 2) cgroups to limit hardware resources (CPU/RAM) . This allows for this "packaging" and kind of "sandbox". In addition, it adds a file-saving feature by using a layering filesystem.
Please watch this awesome presentation: https://www.youtube.com/watch?v=zGw_xKF47T0