Images comprise multiple layers, and most of the time, there is a relationship between various versions of containers that run on your server. With time, new versions of images roll out in your production environment, so removing the old images by doing some housekeeping is best. This will reclaim some valuable space the container images occupy, resulting in a cleaner filesystem.

To remove a particular image, you can use the docker rmi command:

$ docker rmi nginx

Error response from daemon: conflict: unable to remove repository reference “nginx” (must force) – container d5c84356116f is using its referenced image f9c14fe76d50

Oh! We get an error, but why? It’s because we have a container running and using this image.

Tip

You cannot remove images currently used by a running container.

First, you will have to stop and remove the container. Then, you can remove the image using the preceding command. If you want to do everything at once, you can force removal by using the -f flag, which will stop the container, remove it, and then remove the image. So, unless you know what you are doing, do not use the -f flag:

$ docker rmi -f nginx

Untagged: nginx:latest

Untagged: nginx@sha256:af296b18…

Deleted: sha256:f9c14fe7…

We built our container many times, but what should we do if we need to push it to Docker Hub or other registries? But before we do that, we will have to authenticate it with Docker Hub using the following command:

$ docker login                                                                 

Now, you can push the image to Docker Hub using the following command:

$ docker push <your_dockerhub_user>/nginx-hello-world:latest

The push refers to repository [docker.io/<your_dockerhub_user>/nginx-hello-world]

2b7de406bdcd: Pushed

5f70bf18a086: Pushed

845348333310: Pushed

96a9e6a097c6: Pushed

548a79621a42: Mounted from library/ubuntu

latest: digest: sha256:11ec56f0… size: 1366

This has pushed four layers and mounted the rest from Ubuntu. We used Ubuntu as the base image, which is already available on Docker Hub.

If you have multiple tags for the image and you want to push all of them, then you can use the -a or –all-tags option in the push command. This will push all the tags for that particular image:

$ docker push -a <your_dockerhub_user>/go-hello-world

The push refers to repository [docker.io/<your_dockerhub_user>/go-hello-world] 9d61dbd763ce: Pushed

5f70bf18a086: Mounted from <your_dockerhub_user>/nginx-hello-world

bb01bd7e32b5: Mounted from library/alpine

multi_stage: digest: sha256:9e1067ca… size: 945

445ef31efc24: Pushed

d810ccdfdc04: Pushed

5f70bf18a086: Layer already exists

70ef08c04fa6: Mounted from library/golang

41cf9ea1d6fd: Mounted from library/golang

d4ebbc3dd11f: Mounted from library/golang

b4b4f5c5ff9f: Mounted from library/golang

b0df24a95c80: Mounted from library/golang

974e52a24adf: Mounted from library/golang

single_stage: digest: sha256:08b5e52b… size: 2209

When your build fails for some reason and you make changes to your Dockerfile, it’s possible that the old images’ layers will remain dangling. Therefore, it is best practice to prune the dangling images at regular intervals. You can use docker images prune for this:

$ docker images prune

REPOSITORY  TAG  IMAGE ID  CREATED  SIZE

In the next section, we’ll look at another way to improve Docker image efficiency: flattening Docker images.

Leave a Reply

Your email address will not be published. Required fields are marked *