|
| 1 | +# **What is Docker?** |
| 2 | +Docker is a **containerization platform** that packages software (and its dependencies) into lightweight, portable units called **containers**. Think of containers as "shipping containers" for code: |
| 3 | +- They bundle your app, libraries, tools, and runtime into a single standardized unit. |
| 4 | +- Containers run consistently on any machine with Docker installed, eliminating the "it works on my machine" problem. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +### **Key Features of Docker** |
| 9 | +| Feature | Why It Matters | |
| 10 | +|---------|----------------| |
| 11 | +| **Containers** | Isolated, lightweight environments that run apps with their own OS processes, files, and networks. | |
| 12 | +| **Images** | Blueprints for containers (e.g., the `redis` image defines how Redis runs). | |
| 13 | +| **Dockerfile** | A script to automate image creation (e.g., install Redis, configure ports). | |
| 14 | +| **Portability** | Run the same container on Windows, Linux, or macOS. | |
| 15 | +| **Isolation** | Containers don’t interfere with each other or the host system. | |
| 16 | +| **Scalability** | Easily spin up multiple containers (e.g., for microservices). | |
| 17 | +| **Docker Hub** | A public registry to share/pull pre-built images (e.g., `redis`, `nginx`). | |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +### **Core Docker Terminologies** |
| 22 | +1. **Container** |
| 23 | + A running instance of a Docker image. Think of it as a lightweight virtual machine (but not a full VM!). |
| 24 | + |
| 25 | +2. **Image** |
| 26 | + A read-only template with instructions to create a container (e.g., `redis:latest`). |
| 27 | + |
| 28 | +3. **Dockerfile** |
| 29 | + A text file with commands to build a custom image (e.g., `FROM redis`, `EXPOSE 6379`). |
| 30 | + |
| 31 | +4. **Docker Compose** |
| 32 | + A tool to define and run multi-container apps using a `docker-compose.yml` file. |
| 33 | + |
| 34 | +5. **Volume** |
| 35 | + Persistent storage for containers (saves data even if the container stops). |
| 36 | + |
| 37 | +6. **Network** |
| 38 | + Isolated communication channels between containers (e.g., `bridge`, `host`). |
| 39 | + |
| 40 | +7. **Registry** |
| 41 | + A repository for Docker images (e.g., Docker Hub, private registries). |
| 42 | + |
| 43 | +8. **Daemon & Client** |
| 44 | + - **Docker Daemon**: Background service managing containers/images. |
| 45 | + - **Docker Client**: CLI/GUI tool to interact with the daemon (e.g., `docker run`). |
| 46 | + |
| 47 | +9. **Docker Desktop** |
| 48 | + A GUI tool for Windows/macOS to manage Docker (includes the daemon, CLI, and tools). |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +### **Why Use Docker to Run Redis (Especially Offline)?** |
| 53 | +#### 1. **No Dependency Hell** |
| 54 | + - Redis relies on Linux libraries. Docker bundles Redis with its dependencies, so it runs on **Windows/macOS without conflicts**. |
| 55 | + |
| 56 | +#### 2. **Isolated Environment** |
| 57 | + - Redis runs in a container, separate from your host OS. No risk of breaking your system or other apps. |
| 58 | + |
| 59 | +#### 3. **Portability** |
| 60 | + - Once you have the Redis image, you can run it offline anywhere (no need for internet after setup). |
| 61 | + |
| 62 | +#### 4. **Consistent Setup** |
| 63 | + - The same Redis image works on your laptop, server, or teammate’s machine. |
| 64 | + |
| 65 | +#### 5. **Easy Version Management** |
| 66 | + - Run different Redis versions side-by-side (e.g., `redis:6.2` vs. `redis:7.0`). |
| 67 | + |
| 68 | +#### 6. **Offline-Friendly** |
| 69 | + - After pulling the Redis image once (e.g., `docker pull redis`), you can reuse it offline indefinitely. |
| 70 | + |
| 71 | +#### 7. **Persistent Data (Volumes)** |
| 72 | + - Use Docker volumes to save Redis data to your host machine, even when offline: |
| 73 | + ```bash |
| 74 | + docker run -v redis_data:/data -d redis |
| 75 | + ``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### **How Docker Enables Offline Redis Usage** |
| 80 | +1. **Pull the Image Once** |
| 81 | + Download the Redis image when you have internet: |
| 82 | + ```bash |
| 83 | + docker pull redis |
| 84 | + ``` |
| 85 | + This image is cached locally and reused offline. |
| 86 | + |
| 87 | +2. **Run Redis Without Internet** |
| 88 | + Start Redis offline using the cached image: |
| 89 | + ```bash |
| 90 | + docker run --name my-redis -d redis |
| 91 | + ``` |
| 92 | + |
| 93 | +3. **Persist Data** |
| 94 | + Use volumes to store Redis data on your host machine (no internet needed for storage). |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### **Real-World Analogy** |
| 99 | +Imagine Docker as a **microwave meal**: |
| 100 | +- **Image** = Frozen meal (pre-packaged with everything needed). |
| 101 | +- **Container** = Heated meal (ready to eat). |
| 102 | +- **Volume** = Reusable container storing leftovers. |
| 103 | +- **Docker Hub** = Supermarket where you get frozen meals. |
| 104 | + |
| 105 | +Once you have the meal, you don’t need the supermarket (internet) to eat it! � |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### Docker simplifies running Redis (or any app) by: |
| 110 | +- Eliminating dependency issues. |
| 111 | +- Providing isolation and portability. |
| 112 | +- Enabling offline use after initial setup. |
| 113 | +- Making deployment consistent across environments. |
| 114 | + |
| 115 | + |
| 116 | +## To run Redis on Windows, you can use the following method. Note that Redis is not natively supported on Windows, but these workarounds are effective. |
| 117 | + |
| 118 | +### **Step 1: Install Docker Desktop** |
| 119 | +1. **Download Docker Desktop** |
| 120 | + Go to [Docker Desktop for Windows](https://www.docker.com/products/docker-desktop/) and download the installer. |
| 121 | + |
| 122 | +2. **Install and Configure** |
| 123 | + - Run the installer and follow the prompts. |
| 124 | + - Ensure **WSL 2** (Windows Subsystem for Linux) is enabled during installation (Docker will prompt you if not). |
| 125 | + - After installation, launch Docker Desktop and let it run in the background. |
| 126 | + |
| 127 | +3. **Verify Docker Installation** |
| 128 | + Open PowerShell or Command Prompt and run: |
| 129 | + ```bash |
| 130 | + docker --version |
| 131 | + ``` |
| 132 | + If you see `Docker version XX.XX.X`, you’re good to go! |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### **Step 2: Pull the Redis Image** |
| 137 | +Run this command to download the official Redis image from Docker Hub: |
| 138 | +```bash |
| 139 | +docker pull redis |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +### **Step 3: Start a Redis Container** |
| 145 | +Run Redis in a Docker container with this command: |
| 146 | +```bash |
| 147 | +docker run --name my-redis -p 6379:6379 -d redis |
| 148 | +``` |
| 149 | +- **`--name my-redis`**: Names the container `my-redis` (you can change this). |
| 150 | +- **`-p 6379:6379`**: Maps port 6379 on your Windows machine to the container’s port 6379. |
| 151 | +- **`-d`**: Runs the container in the background (detached mode). |
| 152 | +- **`redis`**: The image to use. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +### **Step 4: Verify the Container is Running** |
| 157 | +Check if the Redis container is active: |
| 158 | +```bash |
| 159 | +docker ps |
| 160 | +``` |
| 161 | +You should see a container named `my-redis` with status `Up`. |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +### **Step 5: Connect to Redis** |
| 166 | +#### **Option A: Use `redis-cli` Inside the Container** |
| 167 | +Run this command to access the Redis command-line interface: |
| 168 | +```bash |
| 169 | +docker exec -it my-redis redis-cli |
| 170 | +``` |
| 171 | +Test the connection with: |
| 172 | +```bash |
| 173 | +127.0.0.1:6379> PING |
| 174 | +``` |
| 175 | +If Redis replies `PONG`, it’s working! 🎉 |
| 176 | + |
| 177 | +#### **Option B: Use a GUI Client (e.g., RedisInsight)** |
| 178 | +Download [RedisInsight](https://redis.com/redis-enterprise/redis-insight/) and connect to `localhost:6379`. |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +### **Key Commands for Managing the Container** |
| 183 | +- **Stop the Container**: |
| 184 | + ```bash |
| 185 | + docker stop my-redis |
| 186 | + ``` |
| 187 | +- **Start the Container Again**: |
| 188 | + ```bash |
| 189 | + docker start my-redis |
| 190 | + ``` |
| 191 | +- **Delete the Container**: |
| 192 | + ```bash |
| 193 | + docker rm my-redis |
| 194 | + ``` |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +### **Persisting Data (Optional)** |
| 199 | +By default, Redis data in Docker is ephemeral. To save data permanently: |
| 200 | +1. **Create a Volume**: |
| 201 | + ```bash |
| 202 | + docker volume create redis_data |
| 203 | + ``` |
| 204 | +2. **Run Redis with the Volume**: |
| 205 | + ```bash |
| 206 | + docker run --name my-redis -p 6379:6379 -v redis_data:/data -d redis redis-server --save 60 1 |
| 207 | + ``` |
| 208 | + - `-v redis_data:/data`: Mounts the volume to the container’s `/data` directory. |
| 209 | + - `--save 60 1`: Saves data to disk every 60 seconds if at least 1 key changes (customize as needed). |
| 210 | + |
| 211 | +--- |
| 212 | + |
| 213 | +### **Troubleshooting** |
| 214 | +- **Docker Not Running**: Ensure Docker Desktop is open and the whale icon 🐋 is in your system tray. |
| 215 | +- **Port Conflict**: If port `6379` is already in use, change the host port (e.g., `-p 6380:6379`). |
| 216 | +- **Connection Refused**: Restart Docker or check the container logs: |
| 217 | + ```bash |
| 218 | + docker logs my-redis |
| 219 | + ``` |
| 220 | + |
| 221 | +--- |
0 commit comments