patterndockerMinor
Can I remove a docker image and still keep the container?
Viewed 0 times
canimagethecontainerdockerkeepremoveandstill
Problem
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.
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.
Solution
There are 2 really major points for Docker containers.
inside the container. Container runs smoothly eveywhere, as long as you have the Docker daemon.
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.
Then you might force the process
But that just untags the image, it's still here.
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.
- 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
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 imageThen you might force the process
docker image rm -f ubuntu:20.04
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Untagged: ubuntu@sha256:8bce67040cd0ae39e0beb55bcb976a824d9966d2ac8d2e4bf6119b45505cee64But that just untags the image, it's still here.
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
1d622ef86b13 3 weeks ago 73.9MBSo 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.
Code Snippets
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 imagedocker image rm -f ubuntu:20.04
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Untagged: ubuntu@sha256:8bce67040cd0ae39e0beb55bcb976a824d9966d2ac8d2e4bf6119b45505cee64docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 1d622ef86b13 3 weeks ago 73.9MBContext
StackExchange DevOps Q#11633, answer score: 7
Revisions (0)
No revisions yet.