Sunday 9 June 2019

Tagging a docker image

In this article we will see how to tag an image in docker.

Docker Image Tag background

Tagging an image gives docker image a version to refer from local repository or docker hub. Though optional, it is highly recommended to have a tag for an image. If you look at the docker image its naming looks like: username/repository:tag


Different ways to tag a docker image
Different ways to tag a docker image


Command to tag an image

There are multiple ways we can tag a docker image. Here are the ways to tag a docker image:

(a) Tagging an image during image creation: 

We can create a tag or multiple tags during image creation. The only limitation is that the docker version should be 1.10 or above. The command is:

docker build -t name1:tag1 -t name2:tag2 -t name2:tag3
    

(b) Tagging an image using image:

Using tag command, we can tag an existing image. There are 2 ways to do that. Using image id or using image name

docker tag myimage tech693/myimage:1.0
    

This will create another copy of image with name "tech693/myimage" with tag "1.0"

Deleting a docker Image

So well you have created image but now want to delete it. docker rmi command is used to delete the image

docker rmi <imagename:tag>

How to rename docker image

Renaming docker image is easy but bit tricky. The idea here is to create another image with tag and delete the older one.

Example

docker tag <oldImage:tag> <newImage:tag>
docker rmi <oldImage:tag>

You will notice that if you tag same docker image with multiple name, its Image id remains same which mean tagging is just a name to refer the Image Id for human readability purpose.

That's all about tagging a docker image. If you have query, please ask in comment section. Thanks


Related Articles:
You may also like:

Manage docker image in Ansible using docker_image module



Build docker image using Ansible


Docker as you know is the most popular containerization platform and Ansible is the leading automation tool. In this post we will see how we can automate / manage docker images with Ansible.

Module:

Ansible provides docker_image module to pull / create docker image.

We will cover two areas of docker image
(a) Pulling images from repository using Ansible
(b) Creating images from dockerfile using Ansible

Pulling images from repository using Ansible:

Lets see how we can pull docker image by using docker_image module. Here I am pulling RabbitMQ image.

Source Code:



[root@test ansible_example]# cat docker-pull.yml

---

- hosts: localhost

  tasks:

  - name: Pull RabbitMQ Image

    docker_image:

      name: rabbitmq

      source: pull



Lets run the above playbook.

Output:




[root@test ansible_example]# ansible-playbook docker-pull.yml



 [WARNING]: Could not match supplied host pattern, ignoring: all



 [WARNING]: provided hosts list is empty, only localhost is available



PLAY [localhost] ***************************************************************************************************************



TASK [Gathering Facts] *********************************************************************************************************



ok: [localhost]



TASK [Pull RabbitMQ Image] *******************************************************************************************************



changed: [localhost]



PLAY RECAP *********************************************************************************************************************



localhost                  : ok=2    changed=1    unreachable=0    failed=0






Explanation:

This will download RabbitMQ image from docker repository. To verify use "docker images" command and we will find RabbitMQ images has been pulled from docker images.

Creating images from dockerfile using Ansible:

Docker provides another way to create images using dockerfile. Ansible also provides way to automate creating docker images using dockerfile.

Creating dockerfile:


FROM ubuntu

RUN apt-get update

RUN apt-get install -y rabbitmq-server



Now lets create playbook to build docker my rabbitmq image from above dockerfile

Source Code:




[root@test ansible_example]# cat docker-build.yml

---

- hosts: localhost

  tasks:

  - name: Build RabbitMQ image

    docker_image:

      path: .

      name: test/my-rabbitmq

      tag: v1

      source: build


In the above code snippet, we are trying to build a docker image located at current path.

Now lets run the playbook.



[root@test ansible_example]# ansible-playbook docker-build.yml



 [WARNING]: Could not match supplied host pattern, ignoring: all



 [WARNING]: provided hosts list is empty, only localhost is available



PLAY [localhost] ***************************************************************************************************************



TASK [Gathering Facts] *********************************************************************************************************



ok: [localhost]



TASK [Build RabbitMQ image] *******************************************************************************************************



changed: [localhost]



PLAY RECAP *********************************************************************************************************************



localhost                  : ok=2    changed=1    unreachable=0    failed=0


Please note you can easily push the local image to repository using Ansible. Here is the playbook example:




[root@test ansible_example]# cat docker-push.yml

---

- hosts: localhost

  tasks:

  - name: Push image

    docker_image:

   name: test/rabbitmq:v1

   repository: localhost:4800/test

   tag: v1

   push: yes

   source: local


Conclusion and Best practices:

Basically you can replace your docker-compose yml file with Ansible easily to manage containers even better. This way you can automate containerized application creation & deployment at one place and when the build triggers and completes, Ansible playbook can automatically deploy containers from one place to multiple node(s)

If you are looking for all the options of  docker_image module, please visit the official link here

Kubernetes is Docker orchestration engine and most devops team uses Kubernetes for docker lifecycle. This article can be helpful if you are looking for large number of container deployment using Ansible and Kubernetes.

That's all for building / managing docker images using Ansible. If you have any query, please mention in comment section. Thanks

Related Articles:
You may also like: