Virtual Machines, Containers and the Cloud
Summary
This chapter introduces virtual machines, containerization, and modern cloud computing. You'll learn about cloud providers, virtual machines, and how to launch Linux instances in the cloud. Master Docker containers from building images to running multi-container applications with Docker Compose. Explore Terraform for infrastructure-as-code to automate cloud deployments.
Concepts Covered
This chapter covers the following 25 concepts from the learning graph:
- Cloud Computing
- Virtual Machines
- Cloud Providers
- AWS
- Google Cloud
- Microsoft Azure
- Cloud Linux Instances
- SSH to Cloud
- Docker
- Docker Images
- Docker Containers
- Docker Commands
- Docker Run
- Docker Build
- Dockerfile
- Docker Compose
- Container Networking
- Container Volumes
- Terraform Basics
- Infrastructure as Code
- Terraform Providers
- Terraform Resources
- Cloud Deployment
- Container Registry
- Microservices
- Cloud Security
- Cost Management
Prerequisites
This chapter builds on concepts from:
- Chapter 15: Networking Fundamentals
- Chapter 16: SSH and Remote Access
- Chapter 18: Storage Devices and System Performance
Welcome to the Cloud! βοΈ
Remember when you had to buy a computer to run software? Those days are fading fast. Today, you can spin up a powerful Linux server in the cloud in under a minute, use it for exactly as long as you need, and pay only for what you use. It's like renting a car instead of buying oneβexcept these cars can be any size, from a bicycle to a rocket ship!
In this chapter, you'll learn: - How cloud computing works and why it changed everything - How to launch your own Linux servers in the cloud - How to package applications in containers that run anywhere - How to describe infrastructure as code (yes, you can version control your servers!)
"There is no cloudβit's just someone else's computer." β Unknown (but technically true!)
That joke gets passed around a lot, but here's the thing: those "someone else's computers" are in massive data centers with better cooling, power, security, and reliability than you could ever afford. The cloud isn't just renting computersβit's accessing world-class infrastructure on demand.
What is Cloud Computing?
Cloud computing is delivering computing servicesβservers, storage, databases, networking, softwareβover the internet ("the cloud"). Instead of buying and maintaining physical hardware, you rent what you need from a cloud provider.
Types of Cloud Services
| Service Type | What You Get | You Manage | Provider Manages |
|---|---|---|---|
| IaaS (Infrastructure) | Virtual machines, storage, networks | OS, apps, data | Hardware, virtualization |
| PaaS (Platform) | Runtime environment | Your code | Everything else |
| SaaS (Software) | Complete applications | Just configuration | Everything |
Examples: - IaaS: AWS EC2, Google Compute Engine, Azure VMs - PaaS: Heroku, Google App Engine, AWS Elastic Beanstalk - SaaS: Gmail, Salesforce, Microsoft 365
For learning Linux, we'll focus on IaaSβspinning up actual Linux virtual machines in the cloud!
Why Cloud Computing?
| Benefit | Description |
|---|---|
| Scalability | Add or remove resources in minutes |
| Cost Efficiency | Pay only for what you use |
| Reliability | Data centers have redundancy built-in |
| Global Reach | Deploy in regions worldwide |
| No Maintenance | Someone else handles hardware failures |
Cloud Pun Time
Why did the developer break up with the cloud? Too many issues with commitmentβthey kept changing their instance! βοΈπ
Virtual Machines: Computers Inside Computers
A virtual machine (VM) is a software-based computer running inside a physical computer. It has its own CPU, memory, storage, and networkβall virtualized.
How VMs Work
1 2 3 4 5 6 7 8 9 10 11 | |
The hypervisor is software that creates and manages VMs, dividing physical resources among them.
VM vs Physical Server vs Container
| Feature | Physical Server | Virtual Machine | Container |
|---|---|---|---|
| Boot time | Minutes | Seconds-Minutes | Milliseconds |
| Size | Huge (GB-TB) | Large (GB) | Small (MB) |
| Isolation | Complete | Strong | Process-level |
| OS overhead | None | Full OS per VM | Shared kernel |
| Portability | None | Limited | Excellent |
Cloud Providers: The Big Three
AWS (Amazon Web Services)
AWS is the largest cloud provider, offering 200+ services. It's the industry leader and what most companies use.
| AWS Service | What It Does |
|---|---|
| EC2 | Virtual machines |
| S3 | Object storage |
| RDS | Managed databases |
| Lambda | Serverless functions |
| EKS | Managed Kubernetes |
Free Tier: 750 hours/month of t2.micro (1 vCPU, 1GB RAM) for 12 months
Google Cloud Platform (GCP)
Google Cloud offers excellent machine learning services and competitive pricing. It powers Google Search, YouTube, and Gmail.
| GCP Service | What It Does |
|---|---|
| Compute Engine | Virtual machines |
| Cloud Storage | Object storage |
| Cloud SQL | Managed databases |
| Cloud Functions | Serverless functions |
| GKE | Managed Kubernetes |
Free Tier: e2-micro instance free forever (limited regions)
Microsoft Azure
Azure is popular in enterprise environments and integrates well with Microsoft products.
| Azure Service | What It Does |
|---|---|
| Virtual Machines | VMs |
| Blob Storage | Object storage |
| Azure SQL | Managed databases |
| Azure Functions | Serverless functions |
| AKS | Managed Kubernetes |
Free Tier: B1S VM free for 12 months
Choosing a Cloud Provider
| Factor | AWS | GCP | Azure |
|---|---|---|---|
| Market share | Largest | Growing | Enterprise-focused |
| Learning resources | Abundant | Good | Good |
| Free tier | Generous | Forever free tier! | 12-month |
| Best for | Everything | ML/AI, Kubernetes | Microsoft shops |
For learning, GCP's forever-free tier is great for experiments, while AWS has the most job opportunities.
Launching Cloud Linux Instances
Let's launch a Linux VM in the cloud! I'll show the concepts that apply to all providers.
Step 1: Choose Your Linux Distribution
Cloud providers offer many Linux options:
| Distribution | Best For |
|---|---|
| Ubuntu | General purpose, most tutorials |
| Amazon Linux | AWS-optimized |
| Debian | Stability |
| CentOS/Rocky | Enterprise/server |
| RHEL | Enterprise with support |
Step 2: Choose Instance Size
| Size | vCPUs | RAM | Use Case |
|---|---|---|---|
| Micro | 1 | 0.5-1GB | Learning, testing |
| Small | 1-2 | 2-4GB | Light workloads |
| Medium | 2-4 | 4-8GB | Web servers |
| Large | 4-8 | 16-32GB | Databases |
| X-Large | 8+ | 32GB+ | Heavy workloads |
Cost Alert!
Cloud resources cost money! A micro instance might be free, but larger instances can cost hundreds per month. Always check pricing and set budget alerts!
Step 3: Configure Security
When launching an instance, you'll configure:
- SSH Key Pair: Generate a key pair for secure login
- Security Group/Firewall: Control which ports are open
- Network Settings: VPC, subnet, public IP
AWS CLI Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
GCP CLI Example
1 2 3 4 5 6 7 8 9 10 11 12 | |
SSH to Cloud: Connecting to Your Instance
Once your instance is running, connect via SSH:
1 2 3 4 5 6 7 8 9 10 11 | |
SSH Config for Cloud Instances
Add your cloud instances to ~/.ssh/config for easy access:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Now connect with just:
1 | |
Docker: Containerization Magic π³
Docker is the most popular containerization platform. It packages applications and their dependencies into portable containers that run anywhere.
Why Docker?
The classic problem: "It works on my machine!"
Docker solves this by packaging everything an application needs: - Application code - Runtime (Python, Node.js, etc.) - Libraries and dependencies - Configuration files
The result? A container that runs identically on your laptop, your friend's laptop, a cloud server, or a Kubernetes cluster.
Docker Architecture
1 2 3 4 5 6 7 8 9 10 11 12 | |
Installing Docker
1 2 3 4 5 6 7 8 9 10 | |
Docker Images: The Blueprints
A Docker image is a read-only template used to create containers. Think of it as a snapshot of a filesystem with everything needed to run an application.
Finding Images
Docker Hub (hub.docker.com) is the main registry for Docker images:
1 2 3 4 5 6 7 8 9 10 | |
Image Naming
1 2 3 4 5 6 7 8 | |
Always Use Tags!
Never use :latest in production. Pin specific versions for reproducibility. nginx:1.25.3 is better than nginx:latest.
Docker Containers: Running Instances
A Docker container is a running instance of an image. You can run multiple containers from the same image.
Container Lifecycle
1 2 3 | |
Docker Commands: Your Container Toolkit
Essential Commands
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Docker Run Options
| Option | Description |
|---|---|
-d |
Run in background (detached) |
-p host:container |
Map ports |
-v host:container |
Mount volumes |
-e VAR=value |
Set environment variable |
--name |
Give container a name |
--rm |
Remove when stopped |
-it |
Interactive terminal |
--network |
Connect to network |
Dockerfile: Building Custom Images
A Dockerfile is a text file with instructions to build a Docker image.
Dockerfile Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Dockerfile Instructions
| Instruction | Purpose |
|---|---|
FROM |
Base image |
WORKDIR |
Set working directory |
COPY |
Copy files into image |
RUN |
Execute command during build |
EXPOSE |
Document which ports are used |
ENV |
Set environment variable |
CMD |
Default command to run |
ENTRYPOINT |
Main executable |
ARG |
Build-time variable |
VOLUME |
Create mount point |
Docker Build: Creating Images
1 2 3 4 5 6 7 8 9 10 11 | |
Multi-Stage Builds
Keep images small by using multi-stage builds:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This creates a tiny nginx image with just the built files, not the entire Node.js toolchain!
Docker Compose: Multi-Container Applications
Docker Compose lets you define and run multi-container applications with a single YAML file.
docker-compose.yml Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
Docker Compose Commands
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Container Networking
Containers can communicate with each other through Docker networks.
Network Types
| Type | Description |
|---|---|
bridge |
Default. Containers on same host can communicate |
host |
Container uses host's network directly |
none |
No networking |
overlay |
Multi-host networking (Swarm) |
Creating and Using Networks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Container Volumes: Persistent Data
By default, container data is lost when the container is removed. Volumes provide persistent storage.
Volume Types
| Type | Syntax | Best For |
|---|---|---|
| Named volume | volume_name:/path |
Databases, persistent data |
| Bind mount | ./host/path:/container/path |
Development, config files |
| tmpfs | tmpfs:/path |
Sensitive data, caches |
Working with Volumes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Container Registry: Sharing Images
A container registry stores and distributes Docker images.
Popular Registries
| Registry | URL | Notes |
|---|---|---|
| Docker Hub | hub.docker.com | Default, public/private |
| GitHub Container Registry | ghcr.io | Integrated with GitHub |
| AWS ECR | ECR | AWS-native |
| Google Container Registry | gcr.io | GCP-native |
| Azure Container Registry | azurecr.io | Azure-native |
Pushing to a Registry
1 2 3 4 5 6 7 8 9 10 11 | |
Microservices: Small, Focused Services
Microservices architecture breaks applications into small, independent services that communicate over networks.
Monolith vs Microservices
| Aspect | Monolith | Microservices |
|---|---|---|
| Deployment | All at once | Independent |
| Scaling | Scale everything | Scale what you need |
| Technology | One stack | Mix and match |
| Complexity | Simpler to start | More complex infrastructure |
| Team size | Smaller teams | Larger, distributed teams |
Example Microservices Architecture
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Each service: - Has its own database - Can be written in different languages - Can be deployed independently - Communicates via APIs (REST, gRPC)
Terraform: Infrastructure as Code
Terraform by HashiCorp lets you define cloud infrastructure in code. Instead of clicking through web consoles, you write configuration files that describe what you want.
Why Infrastructure as Code?
| Benefit | Description |
|---|---|
| Version Control | Track changes in Git |
| Reproducibility | Same config = same infrastructure |
| Documentation | Code is documentation |
| Automation | No manual clicking |
| Review | Team can review infra changes |
Installing Terraform
1 2 3 4 5 6 7 | |
Terraform Basics
Terraform uses HCL (HashiCorp Configuration Language).
Basic Structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Terraform Workflow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Terraform Providers
Providers are plugins that let Terraform interact with cloud platforms and services.
Popular Providers
| Provider | Used For |
|---|---|
| aws | Amazon Web Services |
| Google Cloud Platform | |
| azurerm | Microsoft Azure |
| kubernetes | Kubernetes clusters |
| docker | Docker containers |
| github | GitHub repositories |
Provider Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Terraform Resources
Resources are the building blocks of your infrastructure.
AWS Resources Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
Variables and Outputs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 | |
Cloud Deployment: Putting It Together
Let's deploy a containerized application to the cloud!
Complete Terraform + Docker Deployment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
Cloud Security: Protecting Your Resources
Security in the cloud is a shared responsibility: - Provider: Physical security, infrastructure - You: Configuration, access control, data
Security Best Practices
| Practice | Description |
|---|---|
| Least Privilege | Only grant permissions needed |
| MFA | Enable multi-factor authentication |
| Encryption | Encrypt data at rest and in transit |
| Logging | Enable CloudTrail/audit logs |
| Updates | Keep systems patched |
| Secrets | Use secrets managers, not env vars |
IAM Best Practices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Never Commit Secrets!
1 2 3 4 5 | |
Use environment variables or secrets managers:
1 2 | |
Cost Management: Don't Go Broke!
Cloud costs can spiral out of control. Here's how to stay on budget:
Cost Control Strategies
| Strategy | Description |
|---|---|
| Right-size | Use appropriate instance sizes |
| Spot instances | Up to 90% off for interruptible workloads |
| Reserved instances | 30-72% off for committed usage |
| Auto-scaling | Scale down during low usage |
| Cleanup | Delete unused resources |
Budget Alerts
Set up billing alerts before you start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Terraform Cost Estimation
1 2 3 4 5 | |
Cloud Cost Horror Stories
Students have accidentally run expensive instances for months. Always set budget alerts and clean up resources when done learning!
Review Questions
What is the difference between a virtual machine and a container?
Virtual machines run a complete operating system on virtualized hardware, including their own kernel. Each VM requires its own OS installation, which takes gigabytes of space and seconds to minutes to boot. Containers share the host's kernel and only package the application and its dependencies, making them much smaller (megabytes) and faster to start (milliseconds). VMs provide stronger isolation but more overhead; containers are more efficient but share more with the host.
What is the purpose of a Dockerfile, and what are the key instructions?
A Dockerfile is a text file containing instructions to build a Docker image. Key instructions include: FROM (base image), WORKDIR (set working directory), COPY (copy files into image), RUN (execute commands during build), EXPOSE (document ports), ENV (set environment variables), and CMD (default command to run). The Dockerfile creates a reproducible, versioned way to package applications.
How does Docker Compose simplify running multi-container applications?
Docker Compose uses a YAML file (docker-compose.yml) to define multiple services, networks, and volumes in one place. Instead of running multiple docker run commands with complex options, you run docker compose up to start everything. Compose handles container ordering (depends_on), networking (containers can reach each other by service name), shared volumes, and consistent configuration. This makes development environments reproducible and simplifies local testing of microservices.
What is Infrastructure as Code, and why is Terraform useful for cloud deployments?
Infrastructure as Code (IaC) means defining your infrastructure (servers, networks, databases) in configuration files rather than manually creating them through web consoles. Terraform is useful because: (1) configurations can be version-controlled in Git, (2) the same config always creates the same infrastructure (reproducibility), (3) changes can be reviewed before applying, (4) infrastructure can be created, updated, and destroyed with simple commands, and (5) it works across multiple cloud providers with the same language.
What are container volumes, and why are they necessary for databases?
Container volumes provide persistent storage that survives container restarts and removal. By default, all data inside a container is lost when the container is deleted. Databases need volumes because they store data that must persistβwithout a volume, all your database records would be lost every time you restart the container. Volumes can be named (managed by Docker) or bind mounts (mapped to host directories).
Chapter Summary
You've just leveled up from running Linux on single machines to orchestrating infrastructure across the globe! Let's recap:
Cloud Computing: - IaaS gives you virtual machines on demand - AWS, GCP, and Azure are the major providers - You can SSH into cloud instances just like local machines
Docker Containers: - Package applications with all dependencies - Images are blueprints, containers are running instances - Dockerfile defines how to build images - Docker Compose manages multi-container apps
Infrastructure as Code: - Terraform describes infrastructure in configuration files - Providers connect to cloud platforms - Resources are the building blocks you create - Version control your infrastructure!
Key Skills: - Launching cloud Linux instances - Building and running Docker containers - Writing Dockerfiles and docker-compose.yml - Writing Terraform configurations - Managing cloud costs and security
The cloud and containers are where modern software lives. These skills are essential for DevOps, site reliability engineering, and backend development. You're now ready to deploy applications that can scale to millions of users!
Keep experimentingβbut always set those budget alerts! βοΈπ³π
References
- Docker Official Documentation - Comprehensive guide to Docker installation, commands, Dockerfile syntax, and best practices.
- Docker Hub - Official container registry with millions of pre-built images for applications and services.
- Docker Compose Documentation - Define and run multi-container applications with YAML configuration files.
- AWS Documentation - Complete Amazon Web Services documentation including EC2, S3, and all cloud services.
- Google Cloud Documentation - Official Google Cloud Platform guides for Compute Engine, Cloud Storage, and more.
- Microsoft Azure Documentation - Azure cloud services documentation and tutorials for virtual machines and containers.
- Terraform Documentation - Official HashiCorp guide to infrastructure as code with Terraform.
- AWS Free Tier - Details on AWS free tier offerings including 750 hours of EC2 per month for 12 months.
- Google Cloud Free Tier - Always-free tier with e2-micro instance and $300 credit for new users.
- DigitalOcean: Docker Tutorial - Step-by-step guide to installing and using Docker on Linux.
- Dockerfile Best Practices - Official recommendations for writing efficient, secure Dockerfiles.
- Kubernetes vs Docker Swarm - Comparison of container orchestration platforms.
- AWS CLI Documentation - Command-line interface for managing AWS services from the terminal.
- Terraform AWS Provider - Complete reference for managing AWS resources with Terraform.
- Container Security Best Practices - Docker security recommendations including image scanning and runtime protection.
- Microservices Architecture Guide - Patterns and best practices for building microservices-based applications.
- Multi-Stage Docker Builds - Creating smaller production images by separating build and runtime stages.
- Docker Volumes Tutorial - Managing persistent data in containers with named volumes and bind mounts.
- Infracost - Terraform Cost Estimation - Estimate cloud costs before deploying infrastructure with Terraform.
- TecMint: Cloud Computing Basics - Introduction to cloud computing concepts, service models, and deployment types.