# Day 3: Docker, Docker Compose, and MicroK8s Installation on Ubuntu 22.04

In this guide, we will walk through the installation of **Docker**, **Docker Compose**, and **MicroK8s** on **Ubuntu 22.04**. The commands are tested on the following machine specs:

* **RAM:** 4GB
    
* **CPU:** 2 cores
    
* **Storage:** 25GB
    

---

## 🚀 Docker & Docker Compose Installation on Ubuntu 22.04

### 🔄 Step 1: Update the Machine

```plaintext
sudo apt update -y
```

### 🧹 Step 2: Remove Conflicting Packages

```plaintext
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do 
  sudo apt-get remove $pkg; 
done
```

### 📦 Step 3: Setup Docker’s APT Repository

#### Add Docker's Official GPG Key:

```plaintext
sudo apt-get update
sudo apt-get install ca-certificates curl -y
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
```

#### Add Docker Repository:

```plaintext
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
```

```plaintext
sudo apt-get update
```

### 📥 Step 4: Install Docker & Docker Compose

```plaintext
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
```

### ✅ Step 5: Verify Docker Installation

```plaintext
sudo docker run hello-world
```

### 👨‍💻 Step 6: Run Docker as a Non-Root User

```plaintext
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world
```

Now you can run Docker without `sudo`.

---

## ☸️ Install MicroK8s on Ubuntu 22.04

### 🔄 Step 1: Update the Machine

```plaintext
sudo apt update -y
```

### 📥 Step 2: Install MicroK8s

```plaintext
sudo snap install microk8s --classic --channel=1.32
```

### 👥 Step 3: Add Your User to the MicroK8s Group

```plaintext
sudo usermod -a -G microk8s $USER
mkdir -p ~/.kube
chmod 0700 ~/.kube
```

### 🔄 Step 4: Apply Group Changes

Exit and log in again or run:

```plaintext
exit
```

### ✅ Step 5: Check MicroK8s Status

```plaintext
microk8s status --wait-ready
```

### 🔗 Step 6: Set Kubernetes Alias

```plaintext
alias kubectl='microk8s kubectl'
```

### ☸️ Step 7: Access Kubernetes Cluster

```plaintext
kubectl get nodes
```

---

## 🏗️ Adding a Worker Node to MicroK8s Cluster

### 📥 Step 1: Install MicroK8s on Worker Node

```plaintext
sudo apt update -y
sudo snap install microk8s --classic --channel=1.32
```

### 🖥️ Step 2: Get Join Command from Master Node

```plaintext
microk8s add-node
```

This will provide a join command like:

```plaintext
sudo microk8s join <join-command>
```

> ⚠️ **Important:** Both machines (master and worker) must be on the same network.

### 🔗 Step 3: Run Join Command on Worker Node

```plaintext
sudo microk8s join <join-command>
```

### ✅ Step 4: Verify Node is Joined (on Master Node)

```plaintext
microk8s kubectl get nodes
```

### 🔗 Optional: Set Alias for kubectl

```plaintext
alias kubectl='microk8s kubectl'
```

### ⏹️ To Stop MicroK8s

```plaintext
microk8s stop
```

---

## ✅ Conclusion

You’ve successfully installed **Docker**, **Docker Compose**, and **MicroK8s** on your Ubuntu 22.04 machine. Whether you’re running containers or deploying Kubernetes clusters locally, this guide should help you get started with containerization and orchestration smoothly.
