A Virtual Machine (VM) is essentially a computing environment created out of software that enables running programs just like a physical machine. It works on the concept of creating a "virtual" version of a computer, with dedicated amounts of CPU, memory, and storage that are borrowed from a physical server. The virtual machine is partitioned from the rest of the system, meaning that the software inside a VM cannot interfere with the host computer's primary operating system. The guest operating system runs the Virtual hardware and provides an isolated environment for running applications independently.
Containers in many ways represent a more granular, lower-overhead approach to virtualization than a virtual machine. Each container image is packaged up with application code, all dependencies, system tools, runtime, system libraries, and settings needed to run the containerized applications. They are plugged onto a container engine that sits atop the operating system infrastructure, to become containers at runtime. Containers are mainly used for cloud-native, distributed applications and to package legacy applications for increased portability and deployment simplicity.