Monday, 8 July 2024

Getting Started with Docker: Beginner’s Guide

Getting Started with Docker

 Docker simplifies the process of creating, deploying, and running applications by using containerization.

What is Docker?

Docker is an open-source platform designed to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate an application and its dependencies, ensuring that it runs consistently across different environments.

Key Concepts

Before diving into practical steps, let’s understand some key Docker concepts: - Images: Read-only templates that define how a container should be instantiated. They include everything needed to run an application (code, runtime, libraries, etc.). - Containers: Instances of Docker images that run as isolated processes in user space on the host operating system. - Dockerfile: A script containing instructions on how to build a Docker image. - Docker Hub: A cloud-based registry service for sharing Docker images.

Installation 

To start using Docker, you need to install Docker Desktop on your machine. Follow these steps based on your operating system: Windows and macOS

 1. Download Docker Desktop:

 - Visit the Docker Desktop download page and download the installer for your operating system.

 2. Install Docker Desktop:

 - Run the installer and follow the on-screen instructions.

 3. Verify Installation:

 - Open a terminal or command prompt and run: docker --version - You should see the installed Docker version. Linux

 1. Install Docker Engine: - Follow the official installation guide for Docker Engine based on your Linux distribution. 2. Verify Installation: - Open a terminal and run: docker --version - You should see the installed Docker version.

Your First Docker Container

Let’s create a simple Docker container running an Nginx web server. 1. Pull the Nginx Image:

 - Open a terminal and run: docker pull nginx - This command pulls the Nginx image from Docker Hub to your local machine.

 2. Run the Nginx Container:

 - Start an Nginx container using the pulled image: docker run --name my-nginx -p 8080:80 -d nginx - This command runs the Nginx container, names it my-nginx, maps port 80 of the container to port 8080 of the host, and runs it in detached mode.

 3. Verify the Nginx Container:

 - Open a web browser and navigate to http://localhost:8080. - You should see the Nginx welcome page.

Dockerfile Basics

 A Dockerfile is a script containing instructions to build a Docker image. Let’s create a Dockerfile for a simple Node.js application. 1. Set Up the Project Directory: - Create a new directory for your project and navigate into it: mkdir my-node-app cd my-node-app

 2. Create a Node.js Application: - Initialize a new Node.js project and install Express: npm init -y npm install express - Create an index.js file with the following content: const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, Docker!'); }); app.listen(port, () => { console.log(`App running at http://localhost:${port}`); });

 3. Create a Dockerfile: - In the same directory, create a file named Dockerfile with the following content: # Use the official Node.js image as the base image FROM node:14 # Set the working directory WORKDIR /usr/src/app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code COPY . . # Expose the application port EXPOSE 3000 # Command to run the application CMD ["node", "index.js"] 4. Build the Docker Image: - Build the image from the Dockerfile: docker build -t my-node-app .

 5. Run the Docker Container: - Start a container from the built image: docker run -p 3000:3000 my-node-app

 6. Verify the Application: - Open a web browser and navigate to http://localhost:3000. - You should see the message "Hello, Docker!" Managing Docker Containers Here are some useful commands to manage Docker containers: - List Running Containers: docker ps Shows all running containers.

 - Stop a Container: docker stop <container_id> Stops the specified container.

 - Remove a Container: docker rm <container_id> Removes the specified container.

 - List Docker Images: docker images Lists all Docker images on your local machine.

 - Remove a Docker Image: docker rmi <image_id> Removes the specified Docker image. Happy Dockerizing!

Getting Started With Terraform

 Let’s create a simple Terraform configuration to launch an EC2 instance on AWS on a Linux VM.

 

1. Set Up AWS Provider: 

 On shell - 

 

mkdir my-terraform-project
cd my-terraform-project

 

 Create a file named main.tf and add the following content: 

 

provider "aws" {
region = "us-west-2"
}

 

2. Define an EC2 Instance Resource:

In the same main.tf file, add the following resource definition:


resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Example AMI ID
instance_type = "t2.micro"

tags = {
Name = "example-instance"
}
}

 

3. Initialize Terraform:

Initialize your Terraform project, which will download the necessary provider plugins:

 
terraform init


4. Format and Validate Configuration:

Format your configuration files for readability:


terraform fmt


Validate the configuration for syntax errors:

terraform validate


5. Preview and Apply Changes:

Preview the changes Terraform will make to your infrastructure:


terraform plan


Apply the changes to create the EC2 instance:


terraform apply


When prompted, type yes to confirm.


 

6. Verify the Instance:

 

Go to the AWS Management Console and navigate to the EC2 dashboard to see your new instance running.


7. Managing State


Terraform’s state file, terraform.tfstate, keeps track of the resources it manages. This file is crucial for planning and applying changes accurately.
It’s recommended to store your state file in a remote backend (e.g., AWS S3) for collaboration and reliability.

8. Cleaning Up


To destroy the resources created by your configuration, use the destroy command:


terraform destroy


Type yes when prompted to confirm the destruction of resources.
 

 

 

 

 

 

Friday, 22 May 2020

How to ssh in a shell script




How to make ssh seamless for linux shell scripts





Sometimes we have trouble running ssh via shell script which loops through a huge list of hosts with key authorization and we face many issues like below :


  • a host is highly loaded that holds the session
  • host is not reachable
  • prompting for password 
  • you want to make ssh fail rather than prompt for password if the public key authorization fails 
  • prompting for typing 'yes/no' 


Solution which takes care of the everything above :

ssh -oBatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o PasswordAuthentication=no username@<hostname> "Do Stuff" 






Sunday, 10 May 2020

Kafka Python Create a Kafka Topic




Creating a kafka Topic Using Kafka Python Modules


1. Import python module

import kafka

2. Make Connection

admin_client = KafkaAdminClient(bootstrap_servers=[broker_host])

3. Get list of toipcs

topic_list = []
topic_list.append(NewTopic(name=bucket_name,
num_partitions=num_part, replication_factor=repl_factor))

4. Create Topics
admin_client.create_topics(new_topics=topic_list,validate_only=False)

5. Closing connection
admin_client.close()

Thursday, 29 November 2018

Run command as different user in your shell script

If you have a requirement of running a shell command inside your script without switching to that user, follow this method. This can also be used in a case where 'su - user -c' works on terminal but fails in script.  

Suppose you are logged in as root on your CentOS machine and you want to run your script as root user but in your script there is a particular command which you want to run as different user, please follow this syntax -  

...

sudo - u <username> <command>
...

eg.

sudo - u mahi whoami

Output : mahi





Monday, 26 November 2018

Solution : Yum breaks after python upgrade


 Use this method to make both python 3 & 2 work without breaking yum.  


*   Link your default python path to use python3

# ln -s /usr/bin/python3.6 /usr/bin/python

*   Link python2.x as python2

 # ln -s /usr/bin/python2.7 /usr/bin/python2

*   Edit yum binary  and change the python path to the following -

# vi /usr/bin/yum
#!/usr/bin/python2


Test :

-bash-4.3$ /usr/bin/python -V
Python 3.6.4


-bash-4.3$ /usr/bin/python2 -V
Python 2.7.14


-bash-4.3$ yum update # or anything

Note that the first two you can link as per the default you want to use. Keeping yum separate by changing python path is making sure that it uses python 2x always regardless your default setting.  




 


 




Sunday, 25 November 2018

Python way to query a database and execute a shell command


The following example shows how to connect to a database, query the database and use the result values to perform some operation using python.

In this script, there's a local database which has some employee inventory, We will query some record and send the result as an email notification.


# Including shebang and title 

#!/usr/bin/python
# Python script to query a database and send mail alert using shell command


# Importing modules and Setup mysql connection

# Import Modules

import MySQLdb
import sys
import subprocess

# Setting up DB Connection


connection = MySQLdb.connect (host = "localhost", user = "bingo", passwd = "kurkure", db = "emp_db")


# Cursor Object
cursor = connection.cursor ()

cursor.execute ("select ID,emp_email,manager_email where keyvalue='somevalue';")


# Reading rows and storing to variables 

# Fetching results
data = cursor.fetchall ()
for row in data :
        ID = row[0]
        emp_email = row[1]
        manager_email = row[2]
        print hash,user_email,manager_email;


# Sending email notification by running a shell script

        runn = subprocess.check_call("/<path-to-script>/mail %s %s %s" % (str(row[0]), str(row[1]), str(row[2])), shell=True)

cursor.close ()
connection.close ()

sys.exit()


######## /<path-to-script>/mail script will look like -

'''
echo -e "Hello, \n\n\n Employee notification for - $1) $2  \n\n\n Thanks" | mail -r "do-not-reply@247-inc.net"  -s $"New Record - $2" $3


'''