Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Saturday, 30 November 2024

How to Use Kubernetes LeaderElection for Custom Controller High Availability

How to Use Kubernetes LeaderElection for Custom Controller High Availability

In Kubernetes, high availability and fault tolerance are essential for system reliability. For controllers, LeaderElection is a mechanism that ensures only one instance of a controller operates on a specific task at a time in a multi-replica deployment. This blog delves into the concept of LeaderElection, its importance, implementation, and best practices.


What is Kubernetes LeaderElection?

LeaderElection is a process where multiple replicas of a controller or service coordinate to elect a single leader that performs the primary tasks, while others remain on standby. If the leader fails, another instance is elected to ensure continuity.

Why is LeaderElection Necessary?

  • Prevents duplicate work: Without a leader, multiple controller replicas could simultaneously act on the same resource, leading to conflicts or inconsistencies.
  • Ensures high availability: If the leader fails, a new one is promptly elected, maintaining uninterrupted operation.
Kubernetes LeaderElection Explained: Preventing Conflicts in Multi-Replica Deployments
Kubernetes LeaderElection Best Practices: Achieving Reliable Controller Management



How LeaderElection Works

LeaderElection relies on coordination primitives provided by Kubernetes, typically using ConfigMaps or Leases stored in the API server.

  1. Lease-based LeaderElection:
    The leader acquires a lease by updating a resource (like a ConfigMap or Lease object) with its identity and timestamp.
  2. Health checks:
    The leader continuously updates its lease to indicate it is active.
  3. Failover:
    If the leader fails to update the lease within the specified timeout, other candidates compete to acquire the lease.

Key Components of LeaderElection

1. LeaderElectionConfiguration

A configuration block for enabling leader election in custom controllers or operators.

Example configuration:


leaderElection: true
leaderElectionID: "my-custom-controller-leader-election"
leaderElectionNamespace: "kube-system"
leaseDuration: 15s
renewDeadline: 10s
retryPeriod: 2s


2. Leases API

The Lease resource in the coordination.k8s.io API group is often used for LeaderElection.

Example Lease Object:


apiVersion: coordination.k8s.io/v1
kind: Lease
metadata:
  name: my-leader-election-lease
  namespace: kube-system
spec:
  holderIdentity: instance-1
  leaseDurationSeconds: 15
  renewTime: 2024-01-01T00:00:00Z


How to Implement LeaderElection in Go

LeaderElection can be added to custom controllers using the Kubernetes client-go library.

Setup Code for LeaderElection

  1. Import Required Libraries:

import (
    "context"
    "fmt"
    "os"
    "time"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/leaderelection"
    "k8s.io/client-go/tools/leaderelection/resourcelock"
)

  1. Create a Resource Lock:
    The resourcelock package provides abstractions for Lease or ConfigMap-based locks.


config, err := rest.InClusterConfig()
if err != nil {
    panic(err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
    panic(err)
}

lock, err := resourcelock.New(
    resourcelock.LeasesResourceLock,
    "kube-system", // Namespace
    "my-controller", // Lease name
    clientset.CoreV1(),
    clientset.CoordinationV1(),
    resourcelock.ResourceLockConfig{
        Identity: os.Hostname(),
    },
)
if err != nil {
    panic(err)
}

  1. Start LeaderElection:


leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
    Lock:          lock,
    LeaseDuration: 15 * time.Second,
    RenewDeadline: 10 * time.Second,
    RetryPeriod:   2 * time.Second,
    Callbacks: leaderelection.LeaderCallbacks{
        OnStartedLeading: func(ctx context.Context) {
            fmt.Println("Started leading")
            // Your controller's main logic
        },
        OnStoppedLeading: func() {
            fmt.Println("Stopped leading")
        },
        OnNewLeader: func(identity string) {
            if identity == os.Hostname() {
                fmt.Println("I am the leader now")
            } else {
                fmt.Printf("New leader elected: %s\n", identity)
            }
        },
    },
})


Testing LeaderElection

  1. Deploy your controller with multiple replicas:
    spec:
      replicas: 3
  2. Verify logs to see which instance becomes the leader.
  3. Simulate leader failure by terminating the leader pod and observe failover.

Best Practices for LeaderElection

  1. Use short timeouts carefully:
    Setting a very short lease duration or renew deadline may lead to unnecessary failovers due to temporary network issues.

  2. Avoid leader-specific data persistence:
    If the leader persists state, ensure it is accessible to other instances after a failover.

  3. Monitor LeaderElection health:
    Use metrics and logs to monitor the status of LeaderElection in your cluster.

  4. Leverage Kubernetes RBAC:
    Secure the resources (e.g., Lease or ConfigMap) used for LeaderElection to prevent unauthorized access.


Example Use Cases for LeaderElection

  1. Custom Operators:
    Ensures only one operator instance performs resource reconciliation.

  2. Backup Jobs:
    Ensures only one instance performs a backup at a time.

  3. Distributed Systems Coordination:
    Facilitates leader selection in distributed systems for tasks like coordination or consensus.


Conclusion

LeaderElection is a vital mechanism in Kubernetes for ensuring high availability and preventing conflicts in multi-replica deployments. By following this guide, you can implement LeaderElection in your custom controllers, enhancing their reliability and fault tolerance.

What use cases do you have in mind for LeaderElection? Share your thoughts in the comments!

How to Fix 'RBAC Permissions Denied' Errors in Kubernetes

How to Fix 'RBAC Permissions Denied' Errors in Kubernetes

Role-Based Access Control (RBAC) is an integral part of Kubernetes security, ensuring that users and applications access only the resources they are authorized to. However, encountering RBAC Permissions Denied errors can be frustrating and disruptive. This article will explore common causes of these errors and provide a systematic approach to resolve them.


Understanding RBAC in Kubernetes

RBAC controls access to Kubernetes resources based on roles and bindings.

  • Roles: Define a set of permissions for resources within a namespace.
  • ClusterRoles: Similar to Roles but applicable cluster-wide.
  • RoleBinding: Binds a Role to a user, group, or service account within a namespace.
  • ClusterRoleBinding: Binds a ClusterRole to a user, group, or service account cluster-wide.

When a request is made, Kubernetes checks if the user (or service account) has the necessary permissions via RBAC policies. If the required permissions are missing, Kubernetes returns a 403 Forbidden error with a message like "RBAC: Permissions Denied."

How to Fix 'RBAC Permissions Denied' Errors in Kubernetes
@copyright tech693.com



Diagnosing the Error

Before fixing the issue, you need to diagnose the problem accurately. Here's how:

1. Identify the Request Source

Determine whether the request originates from:

  • A user using kubectl or the Kubernetes API.
  • A pod using its service account.

2. Examine the Error Message

Run the command that triggered the error and review the output. For instance:


kubectl get pods --namespace dev


If denied, you'll see something like:


Error from server (Forbidden): pods is forbidden: User "user@example.com" cannot list resource "pods" in API group "" in the namespace "dev"


Key details to note:

  • User or service account: e.g., user@example.com.
  • Resource: e.g., pods.
  • Namespace: e.g., dev.
  • Verb: e.g., list.

3. Check Current Bindings

List the RBAC bindings for the user or service account:


kubectl get rolebinding,clusterrolebinding --all-namespaces | grep "user@example.com"



Steps to Fix 'RBAC Permissions Denied' Errors

Step 1: Review Current RBAC Policies

Use kubectl auth can-i to simulate the permissions:


kubectl auth can-i list pods --namespace dev

This command outputs whether the user or service account has the required permission.


Step 2: Grant Necessary Permissions

Case 1: Missing Role or RoleBinding

If no RoleBinding exists for the user, create one.

  1. Create a Role: Define a Role for the required namespace:

  2. 
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      namespace: dev
      name: pod-reader
    rules:
    - apiGroups: [""]
      resources: ["pods"]
      verbs: ["get", "list", "watch"]
    
    
    Save this as role.yaml and apply it:
  3. 
    kubectl apply -f role.yaml
    
    

  4. Create a RoleBinding: Bind the Role to the user or service account:

    
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      namespace: dev
      name: pod-reader-binding
    subjects:
    - kind: User
      name: user@example.com
      apiGroup: rbac.authorization.k8s.io
    roleRef:
      kind: Role
      name: pod-reader
      apiGroup: rbac.authorization.k8s.io
    
    

    Save as rolebinding.yaml and apply:


kubectl apply -f rolebinding.yaml 

Case 2: Cluster-Wide Permissions Needed

If the error involves cluster-wide resources, use a ClusterRole and ClusterRoleBinding.

  1. Create a ClusterRole:

    
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: cluster-admin-read-only
    rules:
    - apiGroups: [""]
      resources: ["*"]
      verbs: ["get", "list"]
    
    

    Apply it:

    
    kubectl apply -f clusterrole.yaml
    
    

  2. Create a ClusterRoleBinding:

    
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: cluster-read-only-binding
    subjects:
    - kind: User
      name: user@example.com
      apiGroup: rbac.authorization.k8s.io
    roleRef:
      kind: ClusterRole
      name: cluster-admin-read-only
      apiGroup: rbac.authorization.k8s.io
    
    
    Apply the configuration:
    
    kubectl apply -f clusterrolebinding.yaml
    
    


Step 3: Validate the Changes

Re-run the kubectl auth can-i command:


kubectl auth can-i list pods --namespace dev


This should now return yes.

Test the actual command to confirm the error is resolved:

kubectl get pods --namespace dev



Common Troubleshooting Tips

Incorrect Subject Kind

Ensure the subjects field in RoleBinding or ClusterRoleBinding matches the user type:

  • Use kind: User for individual users.
  • Use kind: ServiceAccount for pods.
  • Use kind: Group for groups.

Namespace Mismatch

Roles are namespace-specific. Ensure the namespace in the RoleBinding matches the namespace you’re working with:


kubectl config set-context --current --namespace=dev


Overly Restrictive Rules

Review the Role's rules section. Ensure the API group, resource, and verb are correctly defined.

Use Logs for Advanced Debugging

Enable verbose output in kubectl:


kubectl get pods --namespace dev --v=9

The logs provide detailed insights into permission issues.


Best Practices to Avoid RBAC Errors

Principle of Least Privilege

Grant only the permissions required for the task. Avoid broad permissions like *.

Use Service Accounts for Applications

Avoid using cluster-admin roles for pods. Instead, create dedicated service accounts with specific permissions.

Regular Audits

Audit your RBAC policies periodically:


kubectl get roles,rolebindings,clusterroles,clusterrolebindings --all-namespaces


Employ Tools for RBAC Management

Use tools like RBAC Manager or kubectl-rbac for easier management.


Conclusion

Fixing RBAC Permissions Denied errors in Kubernetes requires a clear understanding of roles, bindings, and user contexts. By systematically diagnosing and addressing the issue, you can ensure your cluster remains secure while enabling seamless access for users and applications. Following the best practices outlined in this guide will help you maintain robust and manageable RBAC policies.

Thursday, 28 November 2024

Ephemeral Containers in Kubernetes: Understanding the What, Why, and How

Mastering Ephemeral Containers in Kubernetes: A Complete Guide to Debugging and Troubleshooting Pods 

Kubernetes has transformed the way we manage and orchestrate containerized applications, offering scalability, resilience, and robust automation. However, the dynamic and distributed nature of Kubernetes environments often leads to challenges in debugging and troubleshooting running workloads. Enter Ephemeral Containers — a powerful feature designed to bridge the gap between troubleshooting and running containerized applications without disrupting production workloads.

This blog explores what ephemeral containers are, their purpose, how they work, and some practical use cases to illustrate their significance.


What Are Ephemeral Containers?

Ephemeral containers are a special type of container in Kubernetes designed explicitly for troubleshooting and debugging. Unlike regular containers, they are temporary, non-persistent, and injected into an existing pod without restarting it. This allows developers and operators to diagnose issues in a live pod without affecting the ongoing workload.

Key Characteristics:

  1. Temporary Nature: Ephemeral containers exist only for the duration of a debugging session and are removed once their purpose is fulfilled.
  2. Non-Disruptive: They do not require the pod to restart, ensuring zero disruption to the running application.
  3. Debugging Focus: They are primarily intended for attaching debuggers, running diagnostic tools, or executing troubleshooting commands.
  4. No Lifecycle Management: Kubernetes does not manage their lifecycle like regular containers (e.g., they are not restarted if they fail).

Ephemeral containers became generally available in Kubernetes 1.23, evolving from an experimental feature to a valuable tool for production-grade clusters.



Why Do We Need Ephemeral Containers?

Debugging issues in Kubernetes is inherently complex due to the following reasons:

  • Immutable Container Design: Containers are typically immutable, making it challenging to add debugging tools after deployment.
  • Dynamic Environments: Kubernetes environments are highly dynamic, with pods being frequently scaled up or down.
  • Isolation: Pods are isolated from the host system, making it harder to access underlying files or processes.

Traditional debugging methods, such as adding sidecar containers or modifying pod specs, can be disruptive and time-consuming. Ephemeral containers address these issues by providing an on-the-fly mechanism to inspect and debug running pods.


How Do Ephemeral Containers Work?

Ephemeral containers leverage the existing pod's infrastructure to run a temporary debugging container. They are injected using the Kubernetes kubectl debug command, which dynamically updates the pod's specification to include the ephemeral container.

Key Steps:

  1. Command Execution:

    • 
      kubectl debug -it <pod-name> --image=<debugging-image>
      
      

    This command adds an ephemeral container to the specified pod.

  2. Debugging Environment: The ephemeral container runs the specified debugging image, such as busybox, alpine, or a custom image with pre-installed diagnostic tools.

  3. Interaction: Operators can access the ephemeral container's terminal to inspect logs, run diagnostic commands, or analyze network traffic.

  4. Removal: Once the debugging session is over, the ephemeral container is automatically cleaned up.

Example YAML:

Here’s an example of how an ephemeral container can be defined programmatically:


apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: app-container
    image: nginx
  ephemeralContainers:
  - name: debug-container
    image: busybox
    command:
    - sh
    - "-c"
    - "sleep 1d"


Use Cases for Ephemeral Containers

1. Debugging Application Failures

Ephemeral containers are ideal for inspecting application behavior without redeploying or restarting the pod. For example:

  • Diagnosing why an application is unable to connect to a database.
  • Debugging a configuration issue causing the application to crash.

2. Inspecting Network Traffic

Operators can use ephemeral containers to analyze network traffic, DNS resolution, or firewall configurations within a pod. Tools like tcpdump or curl can be executed from the ephemeral container.

3. Adding Debugging Tools

Sometimes, the base container image lacks debugging utilities. Ephemeral containers allow the injection of a debugging image with tools like strace, lsof, or top.

4. Troubleshooting Resource Utilization

Monitoring CPU, memory, or disk usage of a running pod can be challenging. Ephemeral containers provide the flexibility to add diagnostic scripts or utilities for detailed insights.

5. Debugging Cluster-Level Issues

For system pods running Kubernetes components (e.g., kube-proxy or coredns), ephemeral containers can help troubleshoot issues related to cluster networking or DNS resolution.


Best Practices

  1. Use Minimal Images: Choose lightweight debugging images like alpine or busybox to minimize resource usage and attack surface.

  2. Restrict Access: Control who can deploy ephemeral containers using Kubernetes Role-Based Access Control (RBAC) to prevent misuse.

  3. Automate Cleanup: Ensure ephemeral containers are removed after the debugging session to maintain a clean cluster state.

  4. Logging and Monitoring: Log the usage of ephemeral containers for auditing purposes.

  5. Predefined Debugging Images: Maintain a repository of pre-approved debugging images with essential tools.


Limitations of Ephemeral Containers

While ephemeral containers are powerful, they come with certain limitations:

  1. No Restart Policy: Kubernetes does not manage the lifecycle of ephemeral containers.
  2. Not Suitable for Persistent Work: Ephemeral containers are designed for temporary tasks and should not be used as a substitute for regular containers.
  3. Limited Resource Guarantees: They share resources with the parent pod, which might lead to contention in resource-constrained environments.

Future of Debugging in Kubernetes

As Kubernetes evolves, tools like ephemeral containers will continue to play a critical role in improving observability and debugging capabilities. They pave the way for more sophisticated debugging workflows, including automated diagnostics and seamless integration with third-party tools.


Conclusion

Ephemeral containers are a game-changer for troubleshooting Kubernetes applications, offering a non-disruptive, flexible, and efficient way to debug live workloads. Their ability to inspect, analyze, and resolve issues without requiring pod restarts makes them indispensable for Kubernetes administrators and developers alike.

By embracing ephemeral containers and incorporating them into your Kubernetes workflows, you can ensure faster resolution of issues and maintain the high availability and reliability of your applications.

Sunday, 24 November 2024

Step by step guide to resolve Istio 503 NC cluster_not_found on Kubernetes

How to resolve Istio 503 NC cluster_not_found on Kubernetes

The Istio 503 NC cluster_not_found error typically occurs when the service mesh cannot find a destination cluster for routing. This error is usually seen in scenarios involving Istio's Envoy proxy and can be caused by several issues, such as incorrect configuration, missing service discovery, or routing rules misconfiguration.

Here’s how you can troubleshoot and resolve the error:


1. Check Service Discovery

  • Verify the service exists: Ensure the destination service is running in the cluster and is discoverable by Istio.
  • Inspect Istio's service registry:
    
    istioctl proxy-config endpoints <pod-name>.<namespace>
    
    
    • Replace <pod-name> and <namespace> with the name of the pod and its namespace. Ensure the destination service is listed.
Istio 503 nc cluster_not_found example
(created by author using Bing Image Creator)



2. Validate Virtual Service and Destination Rule

  • Confirm that the VirtualService and DestinationRule are properly defined for the service.
  • Verify that the host field matches the actual service name (including the namespace if necessary):
  • 
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: example
    spec:
      hosts:
        - service-name.namespace.svc.cluster.local
      http:
        - route:
            - destination:
                host: service-name.namespace.svc.cluster.local
                port:
                  number: 8080
    
    

    3. Check Sidecar Injection

    • Ensure that sidecar injection is enabled for the namespaces where the source and destination services are running:
      
      kubectl get namespace -L istio-injection
      
      
      • If not enabled, annotate the namespace:

        kubectl label namespace <namespace> istio-injection=enabled --overwrite
    • Confirm the sidecar proxy is running:
      
      kubectl get pods -n <namespace> -o jsonpath='{.items[*].spec.containers[*].name}' | grep istio-proxy
      
      

    4. Verify Connectivity and Endpoints

    • Check if the endpoints of the destination service are correctly registered:
      
      kubectl get endpoints -n <namespace>
      
    • Confirm that the pods backing the service are healthy and ready:
      
      kubectl get pods -n <namespace>
      
      

    5. Inspect Gateway and Ingress Configuration

    • If the error involves traffic coming through an Istio Gateway, ensure the Gateway is properly configured:
      
      apiVersion: networking.istio.io/v1beta1
      kind: Gateway
      metadata:
        name: example-gateway
      spec:
        selector:
          istio: ingressgateway # Use Istio ingress gateway
        servers:
          - port:
              number: 80
              name: http
              protocol: HTTP
            hosts:
              - "*"
      
    • Verify the Gateway and VirtualService are linked correctly.

    6. Check DNS Resolution

    • Ensure the DNS names of the services resolve correctly inside the cluster:
      
      kubectl exec <pod-name> -n <namespace> -- nslookup <service-name>
      

    7. Examine Logs

    • Inspect logs from the sidecar proxy for the source pod:
      
      kubectl logs <pod-name> -n <namespace> -c istio-proxy
      
    • Check the Istio control plane logs:
      
      kubectl logs -n istio-system -l app=istiod
      

    8. Validate Istio Configuration Consistency

    • Ensure Istio's configuration is consistent across namespaces:
      
      istioctl analyze
      
      

    9. Sync or Restart Pods

    • Restart the affected pods to re-trigger service discovery:
      
      kubectl rollout restart deployment <deployment-name> -n <namespace>
      
      

    10. Debug Using Envoy Config

    • Inspect the Envoy configuration of the sidecar proxy:
      
      istioctl proxy-config clusters <pod-name>.<namespace>
      
    • Look for the expected cluster and its configuration.

    Feel free to use comment section for further query on resolving 503 NC cluster not found error on Kubernetes

    Tag:

  • "Troubleshooting Istio 503 Errors: Resolving NC cluster_not_found Effectively"
  • "A Step-by-Step Guide to Fixing Istio 503 NC cluster_not_found Errors"
  • "Understanding and Resolving the Istio 503 NC cluster_not_found Error"
  • "Istio Service Mesh Debugging: Fixing 503 NC cluster_not_found"
  • "Comprehensive Troubleshooting for Istio 503 Errors in Kubernetes"
  • "Istio 503 Errors Demystified: How to Resolve NC cluster_not_found Issues"
  • "Debugging Istio Networking Errors: A Guide to 503 NC cluster_not_found"
  • "Istio retry on 503"
  • "Istio 503 nc cluster_not_found kubernetes"
  • "Istio 503 nc cluster_not_found example"
  • "GET HTTP/1.1'' 503 NC cluster_not_found"
  • "Istio 503 no healthy upstream"
  • "Cluster_not_found envoy"
  • "Istio retry on 503"
  • Saturday, 13 July 2019

    Introduction to Docker Compose

    In this post, we will see how to manager multiple container lifecycle using docker-compose. Let us understand what is docker compose.

    Introduction

    Docker compose is a tool provided by docker to manage multiple containers within a single host. So in case you want to create / start  / stop/ remove / scale up / scale down multiple containers from a single command, docker compose comes handy. But if you are looking for managing multiple containers within multiple host, consider using docker swarm / kubernetes. Docker compose manages container lifecycle within a single host. If your containerized application is hosted on multiple node in non-clustered mode, then you need to use another copy of docker-compose on another host. However I would suggest to look for docker swarm or kubernetes for that to manage it from single point.

    What docker compose can do for you?

    Docker compose can automate your container deployment, re-deployment, undeployment. It is not a tool to solely create docker image (docker build used to create docker image)

    Installation of Docker Compose
    If you are using Windows or Mac, docker compose is already installed as it comes in Docker toolbox. But in case of Linux, We need to first install docker compose.

    YAML Configuration file
    Docker compose provides a configuration file docker-compose.yml where in we need to write yaml script to manager container lifecycle.  Here is the simple example of docker-compose.yml with instruction

    Docker-compose Example
    Docker Compose Example

    Let me explain further on it.
    version : It indicates compose version number
    services: This indicates docker-compose that below is the list of services that needs to be containerized
    <service-name>: Name of the service for reference purpose
    build: Path to docker file from where image to be build to be used to create container
    ports: port mapping from host to container
    volumes: volume mapping from host to container
    image: image name to be used to create container

    Docker compose commands:

    So you now have docker-compose.yml and want to manage lifecycle of containers through it. We can create, start, stop, destroy, scale using the same docker-compose.yml file. 
    Here is the list of commands:
    • docker-compose up - It creates container (if required) and also run the container. Use it with this option ( -d ) to run this daemon in background
    • docker-compose down - Just opposite of up command, it stops all the containers and also removes them.
    • docker-compose start - It starts the container. Please note that if the container does not exist, it will not create a new container. It just starts the stopped container listed in docker-compose.yml
    • docker-compose stop - It stops the running container. It goes through each service mentioned in docker-compose.yml and tries to stop the started container.
    • docker-compose rm - It deletes the stopped container. use it with -f to force delete the container.
    • docker-compose scale - It set the number of containers per service. We can both scale up and scale down the number of containers per service using the same command
    • docker-compose exec - It run the command inside the container. You need to pass container id along with command. For eg. docker-compose exec -i <container_id> ls / home
    • docker-compose pause - It pause the services. This is different from stop in the way that it is like sleeping for sometime and when resume it continues from the point where it is paused. Stop service will kill the container running thread and it will start from the scratch. For example, you have application that prints 2 line of statement. if you pause after printing first line of statement, unpause will continue from there and it will print the second line. In case of stop after first line and restart, it will again print both first and second line of statement
    • docker-compose unpause - Resumes the paused services.
    • docker-compose port - It prints the public port for port binding
    • docker-compose build - It build or rebuild services
    • docker-compose bundle - It generates a docker bundle from compose file
    • docker-compose config - It validates the docker compose file
    • docker-compose create - It creates the services
    • docker-compose images - It list the docker images
    • docker-compose logs - View container output
    • docker-compose top - It views the running container
    • docker-compose version - It displays the version of docker compose installed on the host
    • docker-compose help - It gets help on a command
    Thats all for short introduction to docker compose. If you are using Ansible, you can manage containers lifecycle using docker_compose module. For any query, please mention in comments section. Thanks!!

    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: