Docker Best practices.

Docker has revolutionized the world of containerization, enabling scalable and efficient application deployment.

To make the most of this powerful tool, here are 10 essential Docker best practices:

๐—ฆ๐˜๐—ฎ๐—ฟ๐˜ ๐˜„๐—ถ๐˜๐—ต ๐—ฎ ๐—Ÿ๐—ถ๐—ด๐—ต๐˜๐˜„๐—ฒ๐—ถ๐—ด๐—ต๐˜ ๐—•๐—ฎ๐˜€๐—ฒ ๐—œ๐—บ๐—ฎ๐—ด๐—ฒ: Use minimalist base images to reduce container size and vulnerabilities.

๐—ฆ๐—ถ๐—ป๐—ด๐—น๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€ ๐—ฝ๐—ฒ๐—ฟ ๐—–๐—ผ๐—ป๐˜๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฟ: Keep it simple – one process per container for better isolation and maintainability.

๐—จ๐˜€๐—ฒ ๐——๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฟ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐˜€๐—ฒ: Define multi-container applications in a YAML file for easy management.

๐—ฉ๐—ผ๐—น๐˜‚๐—บ๐—ฒ ๐— ๐—ผ๐˜‚๐—ป๐˜๐—ถ๐—ป๐—ด: Store data outside the container to preserve it, even if the container is removed.

๐—–๐—ผ๐—ป๐˜๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฟ ๐—ข๐—ฟ๐—ฐ๐—ต๐—ฒ๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: Consider Kubernetes or Docker Swarm for managing containers at scale.

๐—ฉ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐—ง๐—ฎ๐—ด๐—ด๐—ถ๐—ป๐—ด: Always tag images with version numbers to ensure reproducibility.

๐—›๐—ฒ๐—ฎ๐—น๐˜๐—ต ๐—–๐—ต๐—ฒ๐—ฐ๐—ธ๐˜€: Implement health checks to monitor container status and reliability.

๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ ๐—Ÿ๐—ถ๐—บ๐—ถ๐˜๐˜€: Set resource constraints to prevent one container from hogging resources.

๐——๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฟ๐—ณ๐—ถ๐—น๐—ฒ ๐—•๐—ฒ๐˜€๐˜ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ๐˜€: Optimize Dockerfiles by minimizing layers and using caching effectively.

๐—ฆ๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ถ๐˜๐˜†: Regularly update images, scan for vulnerabilities, and follow security best practices.

How to Send Email using Shell Script (gmail).

In this article, we will learn how to send email using shell scripting. Sending emails programmatically can be very useful for automated notifications, error alerts, or any other form of communication that needs to be sent without manual intervention. Shell scripting offers a powerful way to achieve this through various utilities and tools available in Unix-like operating systems.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Basic knowledge of Shell scripting.

Step #1:Generate the App Password

First go to your account:

Search forย App Passwordย in the search bar.

Enter your google account password to verify itโ€™s you:

Now Enter the app name for which you wanted to get the app password like Shell Script.

And click on Create to create it:

the password will be generated. Note it down cause will be using it.

Step #2:Create a file in Ubuntu

Open the terminal and use theย nanoย command to create a new file:

nano email.sh

Step #3:Write a Script to Send Emails
Below is a basic script to send an email.
-----------------------------------------

#!/bin/bash

# Prompt the user for input
read -p "Enter your email: " sender
read -p "Enter recipient email: " receiver
read -s -p "Enter your Google App password: " gapp
echo
read -p "Enter the subject of mail: " sub

# Read the body of the email
echo "Enter the body of mail (Ctrl+D to end):"
body=$(</dev/stdin)

# Sending email using curl
response=$(curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
    --mail-from "$sender" \
    --mail-rcpt "$receiver" \
    --user "$sender:$gapp" \
    -T <(echo -e "From: $sender\nTo: $receiver\nSubject: $sub\n\n$body"))

if [ $? -eq 0 ]; then
    echo "Email sent successfully."
else
    echo "Failed to send email."
    echo "Response: $response"
fi
------------------------------------------

Save the file and exit the editor.

Explanation of the Script:

This script is designed to send an email using Gmailโ€™s SMTP server through the command line. Hereโ€™s a step-by-step explanation of each part of the script:

  1. Shebang: Theย #!/bin/bashย line at the beginning of the script indicates that the script should be run using the Bash shell.
  2. User Input Prompts: These lines prompt the user to enter
    • The senderโ€™s email address.
    • The recipientโ€™s email address.
    • The senderโ€™s Google App password (entered silently usingย -sย to hide the input).
    • The subject of the email.
  3. Reading the Email Body: This section prompts the user to enter the body of the email. The user can type the email content and end input by pressingย Ctrl+D. The body of the email is then captured into the variableย body.
  4. Sending the Email: This part of the script usesย curlย to send the email
    • -sย makesย curlย run silently.
    • --url 'smtps://smtp.gmail.com:465'ย specifies the Gmail SMTP server with SSL on port 465.
    • --ssl-reqdย ensures SSL is required.
    • --mail-from "$sender"ย specifies the senderโ€™s email address.
    • --mail-rcpt "$receiver"ย specifies the recipientโ€™s email address.
    • --user "$sender:$gapp"ย provides the senderโ€™s email and Google App password for authentication.
    • -T <(echo -e "From: $sender\nTo: $receiver\nSubject: $sub\n\n$body")ย sends the email content including headers and body.
  5. Handling the Response: This final part checks the exit status of theย curlย command ($?):
    • If the exit status isย 0, it prints โ€œEmail sent successfully.โ€
    • If the exit status is notย 0, it prints โ€œFailed to send email.โ€ and outputs the response fromย curl.

Step #4:Make file executable

Change the file permissions to make it executable using theย chmodย command:

chmod +x email.sh

Step #5:Run the script
Run the script by executing the following command:

./email.sh

You will be prompted to enter the senderโ€™s email, recipientโ€™s email, Google App password, subject, and body of the email:

You will get the message Email sent successfully.

Now check the mail box to see if you receive the mail or not.

Conclusion:

In conclusion, sending emails using shell scripting can greatly enhance your automation tasks, enabling seamless communication and notifications. This approach is especially useful for automated notifications, error alerts, and other routine communications. With the power of shell scripting, you can integrate emails functionality into your workflows, ensuring timely and efficient information exchange.

Source

WP2Social Auto Publish Powered By : XYZScripts.com