# Image
## π§ 1. **Build Image & Check Image**
### β Build image from Dockerfile:
```bash
docker build -t my_image_name .
```
### β List all local images:
```bash
docker images
```
### β Inspect image details:
```bash
docker inspect my_image_name
```
---
# Container
## π 2. **Run Container, Check & Enter**
### β Run a container:
```bash
docker run -d --name my_container_name my_image_name
```
### β List running containers:
```bash
docker ps
```
### β List all containers (including stopped):
```bash
docker ps -a
```
### β Enter running container:
```bash
docker exec -it my_container_name bash
# Or use sh if bash not available:
docker exec -it my_container_name sh
```
### β View container logs:
```bash
docker logs my_container_name
```
---
# Volume
## π¦ 3. **Volume Setup, Functioning, Management**
### β List all volumes:
```bash
docker volume ls
```
### β Inspect specific volume:
```bash
docker volume inspect my_volume_name
```
### β Bind-mount or use named volume in run:
```bash
docker run -v my_volume_name:/path/in/container ...
```
### β Check if volume data persists:
* Create a file inside the mounted path in container.
* Stop container.
* Start new container with same volume and verify the file.
---
# nginx
## π 4. **Check Nginx Setup**
### β Run nginx container with config:
```bash
docker run -d -p 80:80 --name nginx_container nginx
```
### β Check container logs (for errors):
```bash
docker logs nginx_container
```
### β Test from host machine:
```bash
curl http://localhost
```
### β Test Nginx from inside container:
```bash
docker exec -it nginx_container curl localhost
```
---
# mairaDB
## π’ 5. **Check MariaDB Setup**
### β Run MariaDB with env variables:
```bash
docker run -d --name mariadb_container -e MYSQL_ROOT_PASSWORD=yourpass mariadb
```
### β Check logs:
```bash
docker logs mariadb_container
```
### β Connect via client:
```bash
docker exec -it mariadb_container mysql -uroot -p
```
### β List databases:
```sql
SHOW DATABASES;
```
---
# wordpress
## π 6. **Check WordPress Setup**
### β Run WordPress linked to MariaDB:
Ensure WordPress has correct `WORDPRESS_DB_HOST`, `WORDPRESS_DB_NAME`, etc.
```bash
docker run -d --name wp_container -p 8080:80 \
-e WORDPRESS_DB_HOST=mariadb_container:3306 \
-e WORDPRESS_DB_USER=wpuser \
-e WORDPRESS_DB_PASSWORD=wppass \
-e WORDPRESS_DB_NAME=wordpress \
wordpress
```
### β Check logs:
```bash
docker logs wp_container
```
### β Access WordPress mainpage:
```bash
# use command to check
curl (-k) http://yilin.fr.42
# OR
# launch chromium browser in terminal
chromium http://yilin.fr.42
```
### β Access WordPress login page (admin):
```bash!
# launch chromium browser in terminal
chromium http://yilin.fr.42/wp-admin
```
log in with credentials set in .env:
```bash!
# WP_URL=https://yilin.42.fr
# WP_TITLE=My Inception Site
# WP_ADMIN_USER=yilin
# WP_ADMIN_PASSWORD=happybirthday
# WP_ADMIN_EMAIL=yilin@student.42.fr
# WP_USER=hottie
# WP_USER_PASSWORD=2hot2handle
# WP_USER_EMAIL=hottie@yilin.42.fr
```
> with the settings of username & password, should be able to log in and access to wordpress admin page
---
# redis (bonus)
## π 7. **Check Redis Setup**
### β Run Redis:
```bash
docker run -d --name redis_container redis
```
### β Connect to Redis CLI:
```bash
docker exec -it redis_container redis-cli
```
> this command initiate a promt
### β Test Redis commands:
```bash
SET [key] [value]
GET [key]
# Example input/output:
SET iam hottie
GET iam
> "hottie"
```
> this means successfully created a key&value in cache
# adminer (bonus)
## β go to adminer site
```bash
# launch chromium browser in terminal
chromium http://yilin.fr.42/adminer
```
## β log in with credentials set in `.env`
```bash
# MYSQL_HOSTNAME=mariadb
# MYSQL_DATABASE=wordpress
# MYSQL_USER=yilin
# MYSQL_ROOT_PASSWORD=happybirthday
# MYSQL_PASSWORD=happybirthday
# on adminer login page:
System: MySQL / mariaDB
Server: mariadb
Username: yilin
Password: happybirthday
Database: wordpress
```
---
# Cleanups
## π§Ή 8. **Final Cleanup Commands**
### β Stop all containers:
```bash
docker stop $(docker ps -q)
```
### β Remove all containers:
5. πΎ What Is Volume & Why Use It?
```bash
docker rm $(docker ps -aq)
```
### β Remove all images:
```bash
docker rmi $(docker images -q)
```
### β Remove all volumes:
```bash
docker volume rm $(docker volume ls -q)
```
### β Remove dangling (unused) data:
```bash
docker system prune -a --volumes
```
---
# General important concept briefs
## π **How Do Nginx, MariaDB, WordPress, Redis, Adminer Work Together?**
```plaintext
+-------------+ +------------+ +----------+
| Browser | ---> | Nginx | <---> | WordPress|
+-------------+ +------------+ +----------+
| |
+--------+ +--------+
|MariaDB | | Redis |
+--------+ +--------+
|
+--------+
|Adminer|
+--------+
```
* **Nginx**: Web server. Routes requests to WordPress.
* **WordPress**: PHP app. Handles website logic.
* **MariaDB**: Stores WordPress data (posts, users).
* **Redis**: Speeds up WordPress by caching.
* **Adminer**: Lets you log into MariaDB and manage data.
> All are connected using **Docker Compose**, each in their own **container**, talking through Dockerβs **network**.
---
## π **Analogy for Dockerfile, Image, Container, Docker Compose, App, Environment**
### **π Analogy: Building and Running a Restaurant**
* **Dockerfile** = *Recipe Book*
β Tells you how to cook a dish (build your app image).
* **Image** = *Prepared Meal in a Box*
β The result of following the recipe. Itβs ready to be served but not βeatenβ yet.
* **Container** = *Table where the meal is served*
β It runs the image (meal), isolated from other tables. Each container is like its own customer with its own plate.
* **Docker Compose File** = *Restaurant Manager*
β Organizes many meals (services) and tells the kitchen when and how to cook and serve each dish. Manages multi-container apps.
* **App** = *The dish itself*
β Your website, database, etc., running inside containers.
* **Environment** = *Restaurant theme/config*
β Custom settings: e.g., lighting, music, menu (ENV variables like DB name, passwords, etc.)
---
## π³ vs π₯ **Docker vs Virtual Machine**
| Feature | Docker (Container) | Virtual Machine (VM) |
| -------------- | --------------------------------- | --------------------------------- |
| Boot time | Seconds | Minutes |
| Resource usage | Lightweight | Heavy (full OS) |
| Isolation | Shares OS kernel | Full OS isolation |
| Use case | Run apps in isolated environments | Full OS testing or legacy systems |
**Analogy**:
Docker is like using multiple apps in the same phone.
VM is like giving every app its own phone.
---
## **What is Nginx?**
**Nginx** (pronounced "engine-x") is a high-performance web server and reverse proxy server. It is widely used for serving static content, load balancing, handling high concurrency, and acting as a gateway to backend applications.
**Why Use It:**
* **High performance and scalability** β Efficiently handles thousands of simultaneous connections.
* **Load balancing** β Distributes traffic across multiple servers.
* **Reverse proxying** β Secures and accelerates backend services.
* **Static file serving** β Excellent for serving HTML, CSS, JS, and media content.
* **Resource efficiency** β Low memory usage compared to alternatives like Apache.
---
## **What is WordPress?**
**WordPress** is a free and open-source content management system (CMS) primarily used to create and manage websites and blogs. It is built on PHP and uses MySQL for data storage.
**Why Use It:**
* **User-friendly interface** β No coding skills needed for basic use.
* **Customizability** β Thousands of themes and plugins.
* **Large community and support** β Extensive documentation and forums.
* **SEO-friendly** β Built-in and plugin-based optimization tools.
* **Flexible use cases** β From blogs and portfolios to e-commerce and enterprise sites.
---
## π’ **What is MariaDB?**
* MariaDB is an **open-source database server**, a drop-in replacement for MySQL.
* It **stores structured data** β like WordPress posts, users, comments.
* Think of it as the **digital filing cabinet** for your application.
---
## πΎ **What Is Volume?**
#### πΈ A Volume is like an **external hard drive** connected to your container.
#### β **Purpose**:
* Store data **persistently** (so it survives even if the container is deleted).
* Separate **app logic** from **app data**.
#### π§± **Relation to directories**:
* You mount volumes to specific folders in the container (e.g., `/var/lib/mysql` for MariaDB).
#### π Volume Usage Examples:
* **MariaDB**: volume stores the database files.
* Without a volume: database is deleted when the container stops.
* **WordPress**: volume stores uploads, themes, plugins.
---
## β‘ **[BONUS] What Is Redis?**
* Redis is an **in-memory key-value store** (NoSQL).
* Commonly used as:
* A **cache** (speed up WordPress)
* A **session store** (store user login sessions)
> Imagine it as a **super-fast notebook** your app can use for temporary, frequently-accessed data.
---
## π **[BOUNS] What Is Adminer?**
* **Adminer** is a lightweight web-based **database management tool**.
* Think of it like **phpMyAdmin**, but simpler and faster.
* You use it to:
* Log into your database
* View tables
* Run SQL commands
* Debug your WordPress DB setup
> It connects directly to MariaDB to help manage your data visually.