# Manage labels on docker(container and image)

|

In this episode, I’ll show you how to add labels on Images and Containers, and filtering them with label tags. When you finished the preceding articles, you may encounter problems that it’s too hard to manage enormous containers and images. Hence, you probably desire to classes those objects(such as containers, images, volumes, and networks) into different types, just for you can found them quickly when you need them.

Here is the label tag. You can put different labels on different objects. For example, you have two containers, one for the testing and one for production purposes. In this scenario, you can label them to different tags, for example, one marked as “test” and the other marked as “production”.

You can add labels on either creating or on existing objects.

First, let’s look at how to add labels on images. Add the following snippet to your Dockerfile.

After you edit and save successfully, then rebuild your docker image.

If you want to view your added labels, use the following command.

Labels also can legitimately be applied to Containers. You can use the following code to attach labels to your containers.

The directive -l and -label are the same meanings. To add multiple labels, repeat the label flag (-l or --label). Such as:

# Filter by labels

When you have applied labels on images and containers, you maybe want filtering with those labels. The --filtering flag can help you archive this purpose. For example, the following snippet only shows dangling images:

It also permitted to filter custom labels, with format “label=value”.

You can do the samething on containers also, check the following snippet:

If you add a directive -a on the end, then it filter in all containers, including stopped containers.

# Update labels

It’s easy to update labels on Images. You only need to change the Dockerfile contents, edit the LABEL value, and then rebuild your image.

However, it’s a little doozy to update labels on containers. It is because of that changing the settings or code running inside a container involves deleting and restarting the container. And the labels are inside the containers, So, if you want to update your container labels, need to delete or stop the running containers that you want to update labels, and then start a new one with new labels.