Docker: Tag Image – Build with Tag, Remove, Re-Tag

The good practice is to tag Docker images with useful tags, e.g. version, intended destination (prod or staging), stability or any other information that is useful when deploying the application in different environments.

In this note i will show how to tag Docker images with one or multiple tags during a build.

Also i will show how to tag already existent Docker image and how to change (retag) or remove tags.

Cool Tip: Enter a running Docker container and start a bash session! Read More →

Tag Docker Image

Once you have a Docker image, you can tag it as follows:

$ docker tag <imageId> <repoName>/<imageName>:<tagName>

– example –

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
<none>           <none>           83eea1180916        27 seconds ago      6.96MB

$ docker tag 83eea1180916 local/app:0.1

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              83eea1180916        39 seconds ago      6.96MB

Tag Docker image with multiple tags:

$ docker tag 83eea1180916 local/app:staging
$ docker tag 83eea1180916 local/app:stable

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              83eea1180916        39 seconds ago      6.96MB
local/app        staging          83eea1180916        39 seconds ago      6.96MB
local/app        stable           83eea1180916        39 seconds ago      6.96MB

Build Docker Image With Tags

Tag Docker image during a build:

$ docker build -t <repoName>/<imageName>:<tagName> .

– example –

$ docker build -t local/app:latest .

Build Docker image with multiple tags:

$ docker build -t local/app:latest -t local/app:0.1 .

Remove Tag From Docker Image

The docker rmi command serves for deleting Docker images, but if the image is tagged with more than one tag, it will remove not image, but tag:

$ docker rmi <repoName>/<imageName>:<tagName>

– example –

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              884484c99f71        39 minutes ago      6.96MB
local/app        latest           884484c99f71        39 minutes ago      6.96MB

$ docker rmi local/app:latest
Untagged: local/app:latest

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              884484c99f71        39 minutes ago      6.96MB

Re-Tag Docker Image

Change Docker image tag:

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              83eea1180916        15 minutes ago      6.96MB
local/app        staging          83eea1180916        15 minutes ago      6.96MB

$ docker tag 83eea1180916 local/app:latest
$ docker rmi local/app:staging
Untagged: local/app:staging

$ docker images
REPOSITORY       TAG              IMAGE ID            CREATED             SIZE
local/app        0.1              83eea1180916        15 minutes ago      6.96MB
local/app        latest           83eea1180916        15 minutes ago      6.96MB

To change Docker image name or repository, use the same principle.

Cool Tip: Clean up a Docker host by removing unused Docker images! Read More →

Was it useful? Share this post with the world!

Leave a Reply