---
# System prepended metadata

title: Docker Compose
tags: [docker, Linux]

---

# Docker Compose 功用

* 本地開發
* 無須手動設定每個container (use script)
* 相對 kubernetes 簡單
* 一台機器內管理多個docker


# Docker-Compose install

```bash=
# v2.23.3 可替換版本
# 方法1
curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 方法2 (如果確定機器版本)
curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose

# 變更檔案權限
chmod +x /usr/local/bin/docker-compose

# 確定下載版本
docker-compose --version
```

---

# Docker-Compose test

1. 建立目錄
```bash=
[root@centos-vm3 ~]# mkdir -p test_docker_compose
[root@centos-vm3 ~]# cd test_docker_compose/
[root@centos-vm3 test_docker_compose]# 
```

2. 建立檔案
```b=
[root@centos-vm3 test_docker_compose]# vim docker-compose.yml
[root@centos-vm3 test_docker_compose]# cat docker-compose.yml 
version: '3'
services:
  web: 
    image: "centos:httpd"    # 必須是有的image檔(docker的也可以)
    ports: 
      - "8080:80"
    command: "/usr/sbin/apachectl -DFOREGROUND"
```

3. 執行
```dockerfile=
[root@centos-vm3 test_docker_compose]# docker-compose up -d
[+] Running 2/2
 ✔ Network test_docker_compose_default  Created                            0.1s 
 ✔ Container test_docker_compose-web-1  Started                            0.1s 
```

4. 檢查
```dockerfile=
[root@centos-vm3 test_docker_compose]# docker-compose ps
NAME                        IMAGE                   COMMAND                  SERVICE   CREATED              STATUS              PORTS
test_docker_compose-web-1   xuanlin123/centos:web   "/usr/sbin/apachectl…"   web       About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp, :::8080->80/tcp
```
可以查看IP後連過去看看

5. 關閉
```dockerfile=
[root@centos-vm3 test_docker_compose]# docker-compose down
[+] Running 2/2
 ✔ Container test_docker_compose-web-1  Removed                     10.2s 
 ✔ Network test_docker_compose_default  Removed                      0.1s 

# 如果沒在有 docker-compose.yml 檔案的目錄下關閉會報錯
[root@centos-vm3 user]# docker-compose down
no configuration file provided: not found
```

:::warning
如果一般使用者無法使用的話
加上`sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose`
:::

---


