Your First Steps with Docker

Installation: Setting Up Your Workshop

Installing Docker is like setting up a workshop. You need the right tools for your operating system:

Verifying Your Installation

Like testing a new tool, let's verify Docker is working properly:

Terminal
$ docker --version
Docker version 24.0.7, build afdd53b
$ docker info
Client: Docker Engine - Community
Server: Docker Engine - Community
  Containers: 0
  Images: 0
  Server Version: 24.0.7

Hello World: Your First Container

Running your first container is like the "Hello World" of Docker. Let's see what happens behind the scenes:

sequenceDiagram participant You participant Docker participant Local as Local Images participant Hub as Docker Hub participant Container You->>Docker: docker run hello-world Docker->>Local: Check for hello-world image Local-->>Docker: Image not found Docker->>Hub: Pull hello-world:latest Hub-->>Docker: Download image layers Docker->>Local: Store image Docker->>Container: Create & start container Container-->>You: Display hello message Container-->>Docker: Exit successfully

Interactive Containers: Playing in a Sandbox

Containers can be interactive, like having a temporary computer you can experiment with:

Running Web Applications

Let's run a real web server in a container - nginx, one of the world's most popular web servers:

graph LR subgraph "Your Computer" A[Browser
localhost:8080] B[Docker] end subgraph "Container" C[nginx
Port 80] end A -->|Request| B B -->|Port mapping
8080:80| C C -->|Response| B B -->|Web page| A style A fill:#4caf50 style C fill:#2196f3
Terminal
$ docker run -d -p 8080:80 nginx
3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c
// Now visit http://localhost:8080 in your browser!

Container Management Commands

Think of these commands as your remote control for containers:

DOCKER CONTROL docker ps List running docker ps -a List all docker stop Stop container docker start Start container docker rm Remove container docker logs View logs docker exec Run command docker inspect Show details

Real World Example: Running a Database

Let's run a PostgreSQL database - something that traditionally takes time to install and configure:

Container Lifecycle in Practice

Let's see how containers behave through their lifecycle with a practical example:

stateDiagram-v2 [*] --> Created: docker create Created --> Running: docker start Running --> Paused: docker pause Paused --> Running: docker unpause Running --> Stopped: docker stop Stopped --> Running: docker start Running --> Stopped: Process exits Stopped --> Removed: docker rm Removed --> [*] Running --> Killed: docker kill Killed --> Removed: docker rm

Practical Exercise: Multi-Container Application

Let's run a WordPress site with MySQL - a real-world application setup:

Terminal - WordPress Setup
# Step 1: Create a network for containers to communicate
$ docker network create wordpress-net

# Step 2: Run MySQL database
$ docker run -d \
    --name mysql-db \
    --network wordpress-net \
    -e MYSQL_ROOT_PASSWORD=rootpass \
    -e MYSQL_DATABASE=wordpress \
    -e MYSQL_USER=wpuser \
    -e MYSQL_PASSWORD=wppass \
    mysql:8.0

# Step 3: Run WordPress
$ docker run -d \
    --name wordpress-app \
    --network wordpress-net \
    -p 8000:80 \
    -e WORDPRESS_DB_HOST=mysql-db \
    -e WORDPRESS_DB_USER=wpuser \
    -e WORDPRESS_DB_PASSWORD=wppass \
    -e WORDPRESS_DB_NAME=wordpress \
    wordpress

# Visit http://localhost:8000 - WordPress is ready!

Understanding Container Isolation

Containers are isolated from each other and the host system. It's like having separate apartments in a building:

Container Isolation Host Operating System Container 1 Process: nginx Network: 172.17.0.2 Files: /var/www CPU: 0.5 cores Container 2 Process: node app Network: 172.17.0.3 Files: /app CPU: 1 core Container 3 Process: postgres Network: 172.17.0.4 Files: /data CPU: 2 cores Isolation Benefits: ✓ Security: Containers can't access each other's files ✓ Stability: One crash doesn't affect others ✓ Resource limits: Prevent resource hogging ✓ Network isolation: Private networks per container

Common Patterns and Best Practices

graph TD A[Container Best Practices] --> B[One Process Per Container] A --> C[Use Official Images] A --> D[Don't Run as Root] A --> E[Keep Containers Stateless] A --> F[Use Health Checks] B --> G[Easier to manage] C --> H[Security updates] D --> I[Security hardening] E --> J[Use volumes for data] F --> K[Auto-restart on failure] style A fill:#e3f2fd style B fill:#c8e6c9 style C fill:#c8e6c9 style D fill:#c8e6c9 style E fill:#c8e6c9 style F fill:#c8e6c9

Troubleshooting Tips

When things don't work as expected, here's your debugging toolkit:

Common Issues and Solutions

Container exits immediately?
Check logs: docker logs container-name

Can't connect to service?
Verify port mapping: docker port container-name

Container using too much memory?
Set limits: docker run -m 512m image-name

Need to debug inside container?
Execute bash: docker exec -it container-name bash

Image taking too much space?
Clean up: docker system prune -a

Your Container Cheat Sheet

Key Takeaways

You've learned to:
✅ Install and verify Docker
✅ Run your first containers
✅ Use interactive containers
✅ Expose services with port mapping
✅ Manage container lifecycle
✅ Run multi-container applications
✅ Debug and troubleshoot containers

Remember: Containers are lightweight, isolated environments that start in seconds. They're perfect for development, testing, and production deployments!

What's Next?

Now that you're comfortable running containers, in the next lesson we'll learn how to build your own custom images with Dockerfiles - turning your applications into portable containers!