Docker Compose Command Config
我們在前幾篇的文章只有使用到一個命令 docker compose up -d
但實際上 docker compose 其實跟 docker cli 一樣有提供相當多個命令可以使用
可以使用命令 docker compose help
查看更多詳細的命令資訊
今天要來學習的命令是 docker compose config
也可以輸入別名 docker compose convert
文檔位置
首先先建立起環境
mkdir mycompose && cd $_
vi docker-compose.yml
services:
nginx:
image: nginx
tty: true
stdin_open: true
ports:
- 80:80
docker compose config
這個命令的主要用途是檢查 docker-compose.yml
是否語法正確,也可用來轉換設定檔的格式
我們可以直接輸入 docker compose config
看看效果
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config
name: mycompose
services:
nginx:
image: nginx
networks:
default: null
ports:
- mode: ingress
target: 80
published: "80"
protocol: tcp
stdin_open: true
tty: true
networks:
default:
name: mycompose_default
查看輸出會發現怎麼跟我們當初輸入的內容不太一樣,好像多了一些內容
其實這個輸出才是 docker compose 實際執行的內容,像是網路跟 ports 的寫法
docker compose 其實會幫我們新增一些選填的設定值或修改成更嚴格的格式
接下來我們試著調整 docker-compose.yml
的內容,故意將 services
打成 services1
services1:
nginx:
image: nginx
tty: true
stdin_open: true
ports:
- 80:80
再次使用 docker compose config
進行檢查
發現 docker compose 會幫我們檢查檔案是否有寫錯
像這邊就檢查出我們故意打錯的設定值 services1
是不被允許的
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config
(root) Additional property services1 is not allowed
我們知道在 docker compose 之中除了支援 yml yaml 其實也有支援 json 格式
如果我們拿到一個 docker-compose.yml
想要轉換格式成 json 就可以使用此命令
把剛才的 services1 參數修改成正確的再輸入命令 docker compose config --format json
就會在輸出上顯示轉換後的內容,如果需要保存記得需要輸入 output 參數 和檔名 docker compose config --format json --output docker-compose.json
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config --format json
{
"name": "mycompose",
"services": {
"nginx": {
"image": "nginx",
"networks": {
"default": null
},
"ports": [
{
"mode": "ingress",
"target": 80,
"published": "80",
"protocol": "tcp"
}
],
"stdin_open": true,
"tty": true
}
},
"networks": {
"default": {
"name": "mycompose_default",
"ipam": {},
"external": false
}
}
}
最後是查詢用途的參數,如果在一個很大的設定檔中如果想要直接看到此設定檔提供了什麼服務,可以直接使用以下命令
- docker compose config --images
- docker compose config --services
- docker compose config --volumes
我們調整一下設定檔的內容
services:
db:
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
# If you really want to use MySQL, uncomment the following line
#image: mysql:8.0.27
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
wordpress:
image: wordpress:latest
volumes:
- wp_data:/var/www/html
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:
wp_data:
可以很快速的列出設定檔內部提供了什麼內容,十分方便
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config --images
mariadb:10.6.4-focal
wordpress:latest
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config --services
db
wordpress
[node1] (local) root@192.168.0.18 ~/mycompose
$ docker compose config --volumes
wp_data
db_data
Summary
今天學了新的命令 docker compose config
,算是十分方便的命令
特別是在開發過程中可以快速檢查有沒有輸入錯誤之後即時進行修正