---
# System prepended metadata

title: Docker 與整合環境
tags: [第一組]

---

---
title: Docker 與整合環境
tags:
- 第一組
---
# Docker 與整合環境

## Docker

### 架構
```
malware_explorer/
├── app/
│   ├── main.py        ← API 主入口
│   ├── models.py      ← 資料表定義（SQLAlchemy）
│   ├── database.py    ← 負責連線資料庫
├── Dockerfile         ← 建構 backend 映像檔
├── docker-compose.yml ← 啟動所有服務（DB + API + GUI）
├── .env               ← 存帳號密碼設定
└── requirements.lock  ← Python 需要安裝哪些套件
```
### 🐳 Docker Compose 設定

#### docker-compose.yml
```yaml
services:
  db:
    image: postgres:15
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - pgdata:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    ports:
      - "5050:80"
    depends_on:
      - db
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
    volumes:
      - pgadmin_data:/var/lib/pgadmin

volumes:
  pgdata:
  pgadmin_data:

```

#### .env 設定檔（環境變數）
```
POSTGRES_USER=admin
POSTGRES_PASSWORD=password
POSTGRES_DB=malware_db

PGADMIN_DEFAULT_EMAIL=admin@example.com
PGADMIN_DEFAULT_PASSWORD=password

DATABASE_NAME=malware_db
```

#### 資料表模型（`models.py`）
以下參考[資料庫架構](/wQcffVhrSDGZgAvJIz9QoQ)
```python
from sqlalchemy import Column, String, Text, Integer, BigInteger, ForeignKey, JSON, TIMESTAMP
from sqlalchemy.dialects.postgresql import UUID
from database import Base
import uuid


class Sample(Base):
    __tablename__ = "samples"

    sample_id = Column(String(64), primary_key=True)  # sha256 當主鍵
    original_name = Column(Text)
    md5_hash = Column(String(32), nullable=False)
    sha1_hash = Column(String(40), nullable=False)
    sha256_hash = Column(String(64), nullable=False)
    file_size = Column(Integer, nullable=False)
    file_type = Column(Text, nullable=False)
    platform_id = Column(BigInteger)
    malware_family_id = Column(BigInteger)
    malware_type_id = Column(BigInteger)
    threat_actor_id = Column(BigInteger, nullable=False)
    source_id = Column(BigInteger, nullable=False)
    sample_file_path = Column(Text, nullable=False)
    uploaded_by = Column(UUID(as_uuid=True), nullable=False, default=uuid.uuid4)
    uploaded_at = Column(TIMESTAMP, nullable=False)
    notes = Column(Text)


class Feature(Base):
    __tablename__ = "features"

    feature_id = Column(BigInteger, primary_key=True)
    sample_id = Column(String(64), ForeignKey("samples.sample_id"), nullable=False)
    feature_type_id = Column(BigInteger, nullable=False)
    rule_name = Column(Text, nullable=False)
    rule_content = Column(Text, nullable=False)
    extra_data = Column(JSON)
    created_by = Column(UUID(as_uuid=True), nullable=False)
    created_at = Column(TIMESTAMP, nullable=False)


class User(Base):
    __tablename__ = "users"

    user_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    name = Column(Text, nullable=False)
    email = Column(Text, nullable=False, unique=True)
    password_hash = Column(Text, nullable=False)
    created_at = Column(TIMESTAMP, nullable=False)
```

### 🚀 啟動服務指令

`docker compose up --build`

### 🛠️ 操作資料庫（兩種方式）

可以到 pgadmin 使用 GUI 管理資料庫或指令
```shell
docker compose exec db psql -U postgres -d malware_db
```




