Monday, July 10, 2017

Oracle Linux - removing image from Docker

When using docker you do want to have a basic set of images available to be used to deploy application containers on your Docker engine. In some cases, due to lifecycle management you can come to a point that you do no longer want to have certain images locally. For development and test environments it can be very good to have some older versions available to do some testing on older images. However, for your production machine most people tend to keep it as clean as possible. This means that you have to clean some old things up. Cleaning up is a task that need to be done with care.

A way of doing this is using the dangling future. Dangling will provide you a way of filtering the images you have that are dangling. An example is shown below:

$ docker images --filter "dangling=true"

REPOSITORY          TAG     IMAGE ID            CREATED             SIZE
none                none    8abc22fbb042        4 weeks ago         0 B
none                none    48e5f45168b9        4 weeks ago         2.489 MB
none                none    bf747efa0e2f        4 weeks ago         0 B
none                none    980fe10e5736        12 weeks ago        101.4 MB
none                none    dea752e4e117        12 weeks ago        101.4 MB
none                none    511136ea3c5a        8 months ago        0 B

You can use this in combination with the rmi command (remove image) as shown in the example below:

$ docker rmi $(docker images -f "dangling=true" -q)

8abc22fbb042
48e5f45168b9
bf747efa0e2f
980fe10e5736
dea752e4e117
511136ea3c5a

even though the dangling option provides a good way of doing things it is still possible that it is error prone. Using it to find them is a good idea, using it for automatically remove the images might be causing some issues and is considerd not the best option by a lot of people. Advised is to use the dangling option in combination with simply knowing what is on your Docker enigine and initiate the remove command in a more controlled fashion.

If you want to remove an image you can use the rmi command. In the example below an Oracle Linux image, in this case oraclelinux:6-slim using rmi. First we check which images we have available:

[root@localhost etc]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
oraclelinux         6.8                 6214272b9f34        24 minutes ago      170.4 MB
oraclelinux         6                   7a4a8c404142        2 weeks ago         170.9 MB
oraclelinux         6-slim              aa531a50e156        2 weeks ago         120.6 MB
[root@localhost etc]#

After this we initiate rmi to remove the image we do no longer want to be present, in our example case oraclelinux:6-slim

[root@localhost etc]# docker rmi oraclelinux:6-slim
Untagged: oraclelinux:6-slim
Untagged: oraclelinux@sha256:0ff2303ddec4d664097768b840b6c76af9bfd6f3b49e7be82e09cfad49939c3c
Deleted: sha256:aa531a50e1565c032d1822d361b7510b55cb1be553d3eb2c3e89c928aa9ff5bd
Deleted: sha256:15ee397aafe48f04935592a0c9fd7a0948b83eac1f43c2cf9f27264a41345e88
[root@localhost etc]#

If we now check we can notice that the 6-slim image have been removed from local storage.

[root@localhost etc]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
oraclelinux         6.8                 6214272b9f34        48 minutes ago      170.4 MB
oraclelinux         6                   7a4a8c404142        2 weeks ago         170.9 MB
[root@localhost etc]#

Keeping your local docker engine clean and making sure you remove unused old images is a good practice, partially you can use the dangling option, building a more controlled way in another way might be a better option and a more save way of doing your housekeeping.

No comments: