5

Trying to save space on the server, and I've noticed that the running containers are much smaller in size than the images I've had to pull to create the containers.

I was wondering about the dependency of the running containers on the images and if it it's possible to remove the images and only keep the containers.

4

There are 2 really major points for Docker containers.

  1. 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.
  2. 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 Docker container

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
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Untagged: ubuntu@sha256:8bce67040cd0ae39e0beb55bcb976a824d9966d2ac8d2e4bf6119b45505cee64

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.