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:

Tuesday, 12 March 2019

Introduction to Docker

Definition
Docker is the platform that provides an isolated environment to run/deploy your application in the form of containers. 

It separates the code from the infrastructure so that developer can concentrate on code only and don't worry about the different environment issues.
In Docker, we ship our containers (along with code) and thus if it worked on one deployment environment (say development / test), it will also work on other environment (say production). There is huge advantage of docker in this context, the process of application release will be much faster.

Features / Advantages
Use of docker container bring multiple advantage over traditional application deployment:
(a) Since docker separates code from infrastructure, developer can concentrate on issue related to application only
(b)  As in docker, we ship containers, all the issues we faced due to different environment will not be there
(c) It virtualizes host operating system rather than hardware, so it is much faster than traditional virtual machines

(d) We can run more instance of application using docker rather than virtual machines. Infact we can run containers inside virtual machines also
(e) There is one great advantage for docker is that we can break monolithic application to multiple micro-services, hence each component is independently deployable without affecting others. So, we have the bug in UI, we dont need to build / deploy whole application, only container / microservice for UI needs to be updated.
(f) we can spin up multiple containers in separate isolated environment using docker thus providing a sort of Highly Availability. This comes handy in a single node environment and we need to update application and if we have multiple containers for same application we can down one to update then up it and then update for other containers resulting in no downtime.

Docker is one of the important part of DevOps where it handles code deployment in an intelligent way.

Conclusion
In short, docker comes handy to 
  • Isolate infrastructure / different environment issue, 
  • Faster code deployment, 
  • Faster processing of application using same hardware resource, 
  • Dividing monolithic application to independent deployable microservices,
  • Minimize application downtime to zero,
  • Easy clustering multiple containers to use as HA and Load balancer both.

Wednesday, 2 January 2019

Docker how to resolve failed to read machine uuid: UUID file '/etc/machine-id'

In this post, we will see how to resolve failed to read machine uuid: UUID file '/etc/machine-id' faced during docker container creation out of docker image file.

Introduction:
This problem generally occurs when container is unable to identify machine uuid of the host, due to which it fails to start.

Solution:
To fix this issue we need to generate dbus machine-id.

The steps to generate dbus machine-id is as below:

Include the following line in the dockerfile

RUN dbus-uuidgen > /etc/machine-id


This will fix the problem because it will copy host machine uuid to containers.

That's all for fixing docker issue related to failed to read machine uuid: UUID file '/etc/machine-id' . Stay tuned for more docker related topics. To subscribe on fb. We are running this blog at free of cost, if you like the article, help us by just clicking on any of the book ads below (not required /compulstion to buy).

Related Articles:

You may also like:

Thursday, 13 September 2018

How to avoid receiving same message twice in RabbitMQ

In this post, we will see how to avoid receiving / processing duplicate message more than once in RabbitMQ.

Background:
RabbitMQ is a messaging queue software (also called message broker) that implemented the Advanced Message Queuing Protocol (AMQP). The producer submits message on queue and consumer processes it. But sometimes we use to see the issue with duplicate processing of same message in case where consumer decide to acknowledge message after processing it.

Problem Statement and Solution:
Suppose there is a message that is published into message broker. There can be two scenario in case of consumers for this message type:

  • Single Consumer: 
    • Consumer receives the message and start processing it and dies (or hung) in between: In this case if the acknowledgement has not been send back to RabbitMQ message broker, the message will remain in queue and when the consumer starts again, it will receive the same message. 
      • Solution: In the above case, developer needs to know if the business logic is processed or not. So developer needs to implement status of the message in either database or flat file with the message id. If same message is received after consumer restarts, developer can take a call to process again or not
    • Consumer receives message and processed it but still it receives same message again:
      • Solution: If you are facing this issue, then the problem lies in the heart-beat  timeout between consumer and RabbitMQ. So here consumer has received the message and its thread is processing it but due to missed heart-beat (due to network latency or whatever reason) RabbitMQ assumes the consumer has died and it closes the connection with consumer. Meanwhile as the consumer thread is active, it is still processing the message and completed it successfully but it was unable to acknowledge it back to RabbitMQ. The solution lies in increasing the heart-beat timeout so that it doesn't misses the heart-beat.
  • Multiple Consumer:
    • Consumer receives the message and start processing it and dies (or hung) in between: In this case if the acknowledgement has not been send back to RabbitMQ message broker, the message will remain in queue and it has been send back to another consumer waiting for message
      • Solution: In the above case, developer needs to know if the business logic is processed or not. So developer needs to implement status of the message in either database or flat file with the message id. If same message is received after consumer restarts, developer can take a call to process again or not
    • Consumer receives message and processed it but other consumer receives same message again:
      • Solution: If you are facing this issue, then the problem lies in the heart-beat  timeout between consumer and RabbitMQ. So here consumer has received the message and its thread is processing it but due to missed heart-beat (due to network latency or whatever reason) RabbitMQ assumes the consumer has died and it closes the connection with consumer. Meanwhile as the consumer thread is active, it is still processing the message and completed it successfully but it was unable to acknowledge it back to RabbitMQ. The solution lies in increasing the heart-beat timeout so that it doesn't misses the heart-beat. 
Thats all about problems related to consumer receiving duplicate messages and its possible solutions. If you have query / suggestion, please feel free to post in comment section. Thanks.

Wednesday, 5 September 2018

Max Priority Queue Implementation in Java

In this post, we will see how to implement max priority queue in Java.

Background:
Priority Queue is an non-blocking unbounded queue, the elements of which are arranged in either natural order (if no comparator is provided) or comparator provided as part of constructor. In both scenario, the queue is arranged in ascending order and the head of queue will have least value. Honestly, this does not make sense for Priority Queue to arrange elements in ascending order which can be called as Min Priority Queue

Have the elements arranged in reversed order will make it Max Priority Queue

Implementation:

Now the question arises, how can we make the Priority Queue as Max Priority Queue with the api provided by Java.
Here is the code example for Max Priority Queue in Java


package com.blogspot.tech693;

import java.util.Collections;
import java.util.PriorityQueue;

public class MaxPriorityQueue{

 public static void main(String[] args){
  // Generic Priority Queue -- Natural order
  PriorityQueue pq = new PriorityQueue<>(6);
  pq.add(4);
  pq.add(2);
  pq.add(1);
  pq.add(6);
  pq.add(5);
  
  System.out.println("Generic / Min Priority Queue::" + pq.poll());
  System.out.println("Generic / Min Priority Queue::" + pq.poll());
  
  // Max Priority Queue Example
  PriorityQueue pq1 = new PriorityQueue<>(6, Collections.reverseOrder());
  pq1.add(4);
  pq1.add(2);
  pq1.add(1);
  pq1.add(6);
  pq1.add(5);
  
  System.out.println("Max Priority Queue::" + pq1.poll());
  System.out.println("Max Priority Queue::" + pq1.poll());
 }
 
}



Output:

Generic / Min Priority Queue::1

Generic / Min Priority Queue::2

Max Priority Queue::6

Max Priority Queue::5


Explanation:
In the above code example, we tried to achieve Priority Queue in descending order. I strongly believe the head of the queue should be the highest element. By using Collections.reverseOrder() method we can conver the Priority Queue into Max Priority Queue.

That's all for Max Priority Queue Implementation in Java. If you have questions / suggestions, please mention in comments sections. Thanks. 

Related Articles:

You may also like:

Sunday, 5 August 2018

Java when another new thread comes to synchronized block does it goes to wait state

In this post, I will discuss what happens when another new thread comes to synchronized block and what happens to its state.

Brief Introduction:

Lets start with understanding what is synchronized block. In Java, synchronized block is the set of code under curly brackets which can only be accessed by a thread holding lock of the object on which it is defined. For example

public class TestSynchronizedBlock {
 public void method1(){
  synchronized(this) {
   System.out.println("Inside synchronized block");
   // Do your stuff here
   System.out.println("Exiting synchronized block");
  }
 }
}


Consider the code above, in class TestSynchronizedBlock, inside method1 the synchronized block can be accessed by the thread who is holding lock for current object of TestSynchronizedBlock (as it is holding lock for this i.e. object of TestSynchronizedBlock class. Every Object (instance of class) in Java has one monitor lock (it is the part of Object class, thus is part of any class object) and the thread which first try to enter into synchronized block gets the monitor lock.

Conclusion:

As I said above, the thread which first try to enter into synchronized block has the exclusive monitor lock and it keeps the locks till the thread is executing inside the block. So if Thread A is executing inside synchronized block, what happens when Thread B try to execute. Thread B will go to wait state (waiting for lock) and it will release any other monitor lock it holds, if any. After Thread A will complete executing the synchronized block, it will release the lock and Thread B will get notified and will hold the monitor lock and then start executing (means it will come out of wait state and goes to Execution state). This holds true incase of multiple threads trying to acquire lock. It is worth to mention that If there is more than one thread waiting for lock and in wait state, after current thread completes the execution, only one thread out of waiting n thread will get notified and there is no such sequence as which one with get notified. You can also refer Spin lock for deep dive on locking mechanism.

That's all for synchronized block in context of multiple threads and its status. If you have any query / doubt / suggestion, please drop a comment.