I'm using docker to build images from a docker file. In the process there's some error happened, so the build exit with error code.
When I run docker images
I can see a untagged image. so I tried to remove it docker rmi xxxxx
. But it always fails, it says the images can't be removed because it's used by a stopped container.
So I dig a little deeper. I run docker ps -a
, now I can see a long list of stopped container which are created when the build process fails.
Why there will be container created?? I thought image is like the concept of "class" in programming, and container is instance of the class. Before the image is successfully built, why there'll be instance created? How can I build image without all those stopped containers ?
Each line of your Dockerfile creates an intermediate container to execute the Dockerfile directive for that line.
If the directive succeeds, that will create an intermediate image, which will be the base for the next container to be launch (to execute the next line of your Dockerfile)
If said directive fails, that can leave a container in an Exited state, which in turn will block the intermediate image it was created from.
Simply cleanup all the containers then all the images, and try again.
If you have tried to build repeatedly your Dockerfile, you end up with a collection of intermediate images and containers.
That is why, when my build (finally) succeeds, I always cleanup extra containers, images and (with docker 1.10+) volumes in my build script:
The build process is creating an intermediate for each step in the dockerfile. Like you can see in this example:
To speed up the build process the intermediate container will be used as cache. Those containers will be deleted after a succesful build but when you try to build for multiple times the 'old' intermediate containers and image will still be on your system.
If you wish to keep the intermediate containers after the build is complete, you must use
--rm=false
. By default it'strue
so after a successful build the intermediate containers will be deleted. When a build fails during the use of an intermediate container, than this container will exit. The intermediate 'image for this container' has tags<none>:<none>
and can't be deleted normally because there is an (excited) container which is from this image. You can delete the image by using the -f (force) flag:docker rmi -f image-id