What are the difference between Docker and virtual machines, and how do you optimize a Docker image for size and security?