---
# System prepended metadata

title: Docker-compose
tags: [Docker]

---

## Intro
:::success 
### Dockerfile: 管理單一Container (首字大寫)

### Docker-compose: 管理多個Container (全部小寫)
:::

:::info
![image](https://hackmd.io/_uploads/rkzUvFkD0.png)

>[!Tip][Docker-compose 參考網站](https://devhints.io/docker-compose)
###### 啟動所有的 Docker Container 
```yaml
docker-compose up -d
```
###### 查看 Docker Container 的執行狀態
```yaml
docker-compose ps
```
###### 查看執行的 log
```yaml
docker-compose logs
```
###### 重啟 Docker Container 
```yaml
docker-compose restart
```
###### 停止 docker-compose 執行的所有 Container 
```yaml
docker-compose stop
```
###### 刪除 docker-compose 的所有 Container 
```yaml
docker-compose rm
```
:::

## Examples
:::success
##### Dockerfile
```dockerfile
# docker hub image python:3.13-slim
FROM python:3.13-slim

# docker container inner folder
WORKDIR /app 

# debian package manager
# build-essential for compiling C extensions
# libpq-dev for PostgreSQL support
# rm -rf /var/lib/apt/lists/* to clean up apt cache
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# Pipfile for dependencies
# Pipfile.lock for exact versions
# --ignore-pipfile to use only the locked versions
COPY Pipfile Pipfile.lock ./
RUN pip install pipenv && pipenv install --deploy --ignore-pipfile

# migrations and static files
COPY . .
RUN pipenv run python manage.py makemigrations \
    && pipenv run python manage.py migrate \
    && pipenv run python manage.py collectstatic --noinput

# execute this command when docker run
CMD ["pipenv", "run", "gunicorn", "shopping.wsgi:application", "--bind", "0.0.0.0:8000"]
```
##### docker-compose.yml
```yaml
services:
  web:
    build: .
    container_name: price-compare
    ports:
      - "8000:8000" # Expose port 8000
    environment: # Environment variables
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
      DJANGO_DEBUG: ${DJANGO_DEBUG}
      DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS}
    volumes:
      - ./db.sqlite3:/app/db.sqlite3 # Persist database
    env_file:
      - .env # Load environment variables from .env file
    depends_on:
      - db

```
:::
---
:::success
#### Docker-compose
```yaml
version: "3.4"
services:
    (ServiceName):
        image: ServiceName
        container_name: (containerName)
        ports: (Hose port對應到容器的Docker port)
          - "8888:80"
        volumes: (儲存資料目錄)
          - (外部路徑):(內部路徑)
      # volumes:
          - <host_path>:<container_path>

        environment:
          VARIABLE1: value1
          VARIABLE2: value2
```
:::

:::success
#### Sample docker-compose.yml

```yaml
services:
  mongo:
    image: mongo
    volumes: (將檔案系統目錄由~/docker/hello-db/mongo/db掛載到/data/db)
      - ~/docker/hello-db/mongo/db:/data/db
      - ~/docker/hello-db/mongo/configdb:/data/configdb
    networks: (名稱一樣的會歸納在同一網段避免重複port號衝突、單一管理隔離性佳)
      - db-counter-net
 
  app:
    build:
      context: .
    image: db-counter
    environment: (連線字串)
      MONGODB_URI: mongodb://mongo.db-counter-net:27017/db_counter
    ports:
      - "3000:3000"
    networks:
      - db-counter-net
    depends_on: (依附在這個DB) (optional要再加上Wait Service)
      - mongo
    command: ./wait-service --tcp mongo.db-counter-net:27017 -- node app.js
             (讓MongoDB在HTTP啟動前啟動，使HTTP能順利連接資料庫)
 
networks: (設定服務容器建立後要使用的網路介面)
  db-counter-net:
    name: db-counter-net
```
:::
## Issue
### DB Connect
- Connect to DB by using host.docker.internal
- Or using WSL IPv4 

ref : https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach

ref: https://mileslin.github.io/2019/04/SQL-Server-Container-%E5%BF%AB%E9%80%9F%E5%85%A5%E9%96%80/