After having read this informative blog post, I am left with some more questions about the way Docker treats images.
Firstly, let’s say I’m building an image based on nginx:latest called my-great-site:latest, and I don’t currently have a local copy of nginx. docker build automatically pulls that base image from the Docker Hub, and I can see it in the output of docker image ls:
REPOSITORY TAG IMAGE ID CREATED SIZE
my-great-site latest 99651da2f649 14 hours ago 133MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB
If an image is just a reference to the layers that compose the image, why do I need to have a copy of the nginx image itself in order to build my-great-site? Shouldn’t Docker be able to build my custom image by pulling only the anonymous layers associated with nginx:latest?
Secondly, if my local copy of nginx:latest is outdated by the latest build on Docker Hub, why does docker pull leave a named but untagged copy of the older nginx image in my local image cache? Specifically, why doesn’t it leave the older copy as a dangling image?
REPOSITORY TAG IMAGE ID CREATED SIZE
my-great-site latest 99651da2f649 14 hours ago 133MB
nginx <none> 01a84233ca90 2 weeks ago 132MB
nginx latest 7e4d58f0e5f3 2 weeks ago 133MB