There are 2 really major points for Docker containers.
- You make sure the app dependencies are versioned and encapsulated
inside the container. Container runs smoothly eveywhere, as long as you have the Docker daemon.
- You have the capability to create/destroy a
container on any time, also known as ephemeral property. You could test easily, maintain N of them if needed ( Replica Sets in K8S for example are doing just that, Docker Swarm uses services to maintain this number as well ).
The official Docker docs defines an image as
An image is a read-only template with instructions for creating a
You can't remove an image, because the container relies on it.
docker image rm ubuntu:20.04
Error response from daemon: conflict: unable to remove repository reference "ubuntu:20.04" (must force) - container ecd06b4eb4bf is using its referenced image
Then you might force the process
docker image rm -f ubuntu:20.04
But that just untags the image, it's still here.
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 1d622ef86b13 3 weeks ago 73.9MB
So to your question, the best practice is to only remove images that are not being used by any containers. If you maintain 2-5 versions of your app, you could safely remove the others. Also you schould strive for minimum size of each image, even in total that schould not bother your available space that much.