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.