Docker builds images by creating layers for each command in the Dockerfile. Each RUN command adds a new layer, increasing the image size. Squashing layers means combining multiple layers into a single one to reduce the total image size and number of layers. This helps make images smaller and faster to deploy. The base layer from the FROM command remains unchanged. In the example, installing curl and adding a file create separate layers, which are then squashed into one combined layer. This process reduces overhead and optimizes the image.