日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

docker-compose之环境配置以及服务编排文件的使用讲解

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 docker-compose之环境配置以及服务编排文件的使用讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要講解以下兩塊內容:環境配置的作用及常規使用、服務配置文件的解析以及常規使用

必備知識:

在講解服務配置文件和環境配置文件之前,首先要對docker以及編排工具compose有一定的了解,然后,才能結合服務配置文件編排整個項目的服務以及其依賴關系等。

環境變量:在容器中生效的全局變量值

環境配置文件:可以替換compose服務配置文件中的屬性變量。compose默認讀取環境配置文件為“.env”,也可以通過--env-file指定相應的配置文件

compose file:服務編排文件,主要定義了一系列服務的配置信息以及依賴等,主要包括:端口、鏡像、環境信息、依賴關系等

注意:此處的環境變量和.env類型的環境配置文件是兩個概念。環境變量主要是在compose部署啟動服務容器后,在容器中生效的變量;而.env中配置的變量值,主要是在定義服務配置文件時,去替換相應的屬性。二者的作用范圍不同,前者是服務啟動后,在容器中作用的環境變量,后者是為了方便定義服務配置文件而構建變量文件。

在Compose文件中替換環境變量(.env)

在編輯Compose file時,可以通過環境變量的形式來填充變量的值

web:image: "webapp:${TAG}"

那么如何傳入這些環境變量呢?主要是通過創建.env配置文件并定義相關屬性,然后通過--env-file來指定我們定義的.env配置文件。

.env使用介紹

1.創建項目目錄

mkdir env_test cd env_test

2.創建編排配置文件docker-compose.yml

vi docker-compose.yml#內容如下 services:webapp:image: '${image}:${tag}'

在該配置文件中,我們將image鏡像使用${image}:${tag}替換成對應的變量值

3.創建.env

vi .env#內容如下 image=training/webapp tag=latest

注意:compose默認會從當前項目路徑加載.env配置文件,如果.env在其他目錄的話,需要通過--env-file=/path/.env的方式去加載;同樣的,如果配置文件使用其他命名方式的話,也需要使用--env-file=/path/.env_rename來加載指定配置文件

4.通過docker-compose config來校驗編排配置文件

#默認讀取當前項目路徑的.env [shuchang@docker01 env_test]$ docker-compose config services:webapp:image: training/webapp:latest version: '3.9'#讀取指定配置文件.env_case2 #.env_case2內容如下 image=test tag=1.1[shuchang@docker01 env_test]$ docker-compose --env-file .env_case2 config services:webapp:image: test:1.1 version: '3.9'

可以看到,我們在環境配置文件中定義的變量會作為environment配置項的內容加載到編排配置文件中;同時,通過--env-file指定環境配置文件時,同樣能實現變量的賦值替換。

#在docker-compose.yml中追加ports配置項 services:webapp:image: '${image}:${tag}'ports:- '${host_port}:${container_port}'#并在.env中配置對應的變量 host_port=8080 container_port=80#由于.env_case2中并未定義兩個變量,會拋出異常 [shuchang@docker01 env_test]$ docker-compose --env-file .env_case2 config WARNING: The host_port variable is not set. Defaulting to a blank string. WARNING: The container_port variable is not set. Defaulting to a blank string. ERROR: The Compose file './docker-compose.yml' is invalid because: services.webapp.ports contains an invalid type, it should be a number, or an object#去掉docker-compose.yml中的ports配置項后 [shuchang@docker01 env_test]$ docker-compose --env-file .env_case2 config services:webapp:image: test:1.1 version: '3.9'

在容器中使用環境變量

容器中設置變量,主要是通過在docker-compose.yml中對應的服務下配置environment項,效果等同于docker-compose run -e variable=value

web:environment:- DEBUG=1

同時,還可以在配置env_file配置項,從指定的配置文件中加載環境變量

web:env_file:- web-variables.env

?首先,我們配置docker-compose.yml,在webapp服務下配置env_file配置項,加載文件中的環境變量到webapp服務容器內

vi docker-compose.yml #配置內容如下 services:webapp:image: '${image}:${tag}'env_file:- './webapp.env'

?然后定義配置文件webapp.env

vi webapp.env #內容如下 image=training/webapp tag=latest host_port=8080 container_port=80

最后通過docker-compose config來檢驗配置并輸出,可以看到在webapp.env中定義的屬性都合并到編排文件的environment配置項中了

[shuchang@docker01 env_test]$ docker-compose config services:webapp:environment:container_port: '80'host_port: '8080'image: training/webapptag: latestimage: training/webapp:latest version: '3.9'

Compose file解析及使用

compose file作為compose進行服務編排部署的配置文件,在整個過程中起到了決定性的作用。它可以定義服務的一些基本信息,例如:端口、鏡像、數據卷等等;同時,還能聲明服務之間的依賴關系,決定服務的啟動順序等。因此,了解compose file的常規配置項是十分有必要的!

compose 默認是直接加載docker-compose.yml作為服務的配置文件的,如需指定其他配置文件,需要通過-f /path/docker-compose-customerized.yml來加載配置文件。

Compose file配置項解析

見https://blog.csdn.net/weixin_43762303/article/details/123397714

?profiles配置規劃服務

配置文件允許通過profiles配置項選擇性地啟用服務來針對各種用途和環境調整 Compose 應用程序模型。

version: "3.9" services:frontend:image: frontendprofiles: ["frontend"]phpmyadmin:image: phpmyadmindepends_on:- dbprofiles:- debugbackend:image: ibmcom/backenddb:image: mysql

當前服務配置文件中,frontend和phpmyadmin分別綁定了profiles['frontend']和profiles['debug'];

沒有profiles屬性的服務將始終啟用,即在這種情況下運行docker-compose up只會啟動backend和db。如需啟動上述兩個服務,需要通過--profiles指定相應配置

[shuchang@docker01 profiles_example]$ docker-compose up Starting profiles_example_db_1 ... done Starting profiles_example_backend_1 ... done Attaching to profiles_example_db_1, profiles_example_backend_1 db_1 | 2022-03-10 09:48:30+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. db_1 | 2022-03-10 09:48:31+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' db_1 | 2022-03-10 09:48:31+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. db_1 | 2022-03-10 09:48:31+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified db_1 | You need to specify one of the following: db_1 | - MYSQL_ROOT_PASSWORD db_1 | - MYSQL_ALLOW_EMPTY_PASSWORD db_1 | - MYSQL_RANDOM_ROOT_PASSWORD backend_1 | App listening on port 3001![shuchang@docker01 profiles_example]$ docker-compose --profile debug up Starting profiles_example_db_1 ... done Starting profiles_example_backend_1 ... done Starting profiles_example_phpmyadmin_1 ... done Attaching to profiles_example_backend_1, profiles_example_db_1, profiles_example_phpmyadmin_1 backend_1 | App listening on port 3001! db_1 | 2022-03-10 09:57:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. db_1 | 2022-03-10 09:57:49+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' db_1 | 2022-03-10 09:57:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. db_1 | 2022-03-10 09:57:49+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified db_1 | You need to specify one of the following: db_1 | - MYSQL_ROOT_PASSWORD db_1 | - MYSQL_ALLOW_EMPTY_PASSWORD db_1 | - MYSQL_RANDOM_ROOT_PASSWORD profiles_example_db_1 exited with code 1 phpmyadmin_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.4. Set the 'ServerName' directive globally to suppress this message phpmyadmin_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.22.0.4. Set the 'ServerName' directive globally to suppress this message phpmyadmin_1 | [Thu Mar 10 09:57:50.215499 2022] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.52 (Debian) PHP/8.0.16 configured -- resuming normal operations phpmyadmin_1 | [Thu Mar 10 09:57:50.216222 2022] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

因此,通過在compose file中配置profiles屬性時,可以配置在多種環境下生效的服務,方便快捷測試。

多個Compose file之間的配置共享

默認情況下,Compose 讀取兩個文件,一個docker-compose.yml和一個可選?docker-compose.override.yml文件。

如果兩個文件中都定義了同樣的服務,則會將兩個服務的配置進行合并;若需要加載其他compose file,則需要通過-f /path/docker-compose-other.yml 指定不同名稱的配置文件

注意:

compose針對image、command和mem_limit這種單值的配置項,會直接用新值替換舊值
針對ports、expose、external_links、dns、dns_search和tmpfs這種多值的配置項,會將多個配置項的值合并到一起,且不會覆蓋
針對environment、labels、volumes和devices這幾個多值的配置項,若存在交集的話,會用新的值替換舊的值,其他值合并到一起

樣例:

分別構建兩個配置文件:docker-compose.yml和docker-compose.override.yml

[shuchang@docker01 case1]$ cat docker-compose.yml version: "3.9" services:web:build: .ports:- "8000:5000"volumes:- .:/codeenvironment:FLASK_ENV: developmentredis:image: "redis:alpine"db:image: "mysql"ports:- "23306:3306"command: 'cat ./test.txt'[shuchang@docker01 case1]$ cat docker-compose.override.yml services:db:ports:- 13306:3306

執行docker-compose config 校驗服務編排配置并輸出

[shuchang@docker01 case1]$ docker-compose config services:db:command: cat ./test.txtimage: mysqlports:- published: 23306target: 3306- published: 13306target: 3306redis:image: redis:alpineweb:build:context: /home/shuchang/docker/docker-compose_example/case1environment:FLASK_ENV: developmentports:- published: 8000target: 5000volumes:- /home/shuchang/docker/docker-compose_example/case1:/code:rw version: '3.9'

可以看到,兩個配置文件中配置的db服務已經合并到了一起,同時暴露了23306:3306,13306:3306端口。因此,可以得知compose默認會讀取docker-compose.yml和docker-compose.override.yml,并將配置合并到一起

但是,如果合并配置后存在沖突的話,就會出現異常!例如:上述合并端口時,是以主機的兩個端口映射到了容器的同一個端口,這樣是可行的。但是如果是把主機的同一個端口映射到容器的兩個不同的端口呢?那么必然會導致端口沖突的問題,無法正常啟動!

這里我們修改docker-compose.override.yml文件中的db服務,將ports配置項改為23306:3307,這樣就能復現上述的問題

[shuchang@docker01 case1]$ docker-compose up Starting case1_web_1 ... Starting case1_redis_1 ... Creating case1_db_1 ... Creating case1_db_1 ... error Starting case1_web_1 ... done Starting case1_redis_1 ... donebaa2cfa2fcb378b28919b45cbf67e): Bind for 0.0.0.0:23306 failed: port is already allocatedERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint case1_db_1 (c26e2d7638cf0a470b82295dd0636a9e04fbaa2cfa2fcb378b28919b45cbf67e): Bind for 0.0.0.0:23306 failed: port is already allocated ERROR: Encountered errors while bringing up the project.

通過-f <filename>指定compose file

在使用-f指定compose file時,需注意如果只指定一個文件時,compose只會讀取指定的配置文件,如docker-compose -f docker-compose.origin.yml config,只會加載docker-compose.origin.yml一個文件中定義的服務;而docker-compose -f docker-compose.yml -f docker-compose.origin.yml config 則會將兩個配置文件中的服務進行合并。

[shuchang@docker01 case1]$ cat docker-compose.origin.yml #compose針對image、command和mem_limit這種單值的配置項,會直接用新值替換舊值 #針對ports、expose、external_links、dns、dns_search和tmpfs這種多值的配置項,會將多個配置項的值合并到一起,且不會覆蓋 #針對environment、labels、volumes和devices這幾個多值的配置項,若存在交集的話,會用新的值替換舊的值,其他值合并到一起 services:db:command: '-d'ports:- "3306:3306"web:ports:- "8000:4000"[shuchang@docker01 case1]$ cat docker-compose.yml version: "3.9" services:web:build: .ports:- "8000:5000"volumes:- .:/codeenvironment:FLASK_ENV: developmentredis:image: "redis:alpine"db:image: "mysql"ports:- "23306:3306"command: 'cat ./test.txt'[shuchang@docker01 case1]$ docker-compose -f docker-compose.origin.yml -f docker-compose.yml config services:db:command: cat ./test.txtimage: mysqlports:- published: 3306target: 3306- published: 23306target: 3306redis:image: redis:alpineweb:build:context: /home/shuchang/docker/docker-compose_example/case1environment:FLASK_ENV: developmentports:- published: 8000target: 4000- published: 8000target: 5000volumes:- /home/shuchang/docker/docker-compose_example/case1:/code:rw version: '3.9'

服務擴展之extends

Docker Compose 的extends關鍵字可以在不同的文件甚至完全不同的項目之間共享通用配置。如果存在多個重用通用配置選項的服務,那么extends能發揮關鍵作用。使用extends您可以在一個地方定義一組通用的服務選項,并從任何地方引用它。

注意:volume_from 和 depend_on 無法通過extends進行共享配置,這樣會導致依賴之間的引用沖突;且volume在本地文件定義的話,能夠確保各個服務對于數據卷的內容進行更改后,不會影響其他服務的運作。

extends的使用

首先創建compose file,通過extends引用共享配置

vi docker-compose.yml #內容如下 services:web:extends:file: common-services.ymlservice: webapp

上述文件中通過extends引用共享配置common-services.yml中的webapp服務,因此這里需要創建一個common-services配置文件,并構建共享服務webapp

vi common-services.yml #內容如下 services:webapp:build: .ports:- "8000:8000"volumes:- "/data"

然后,我們通過docker-compose config 校驗服務配置并輸出相應內容

[shuchang@docker01 extends_example]$ docker-compose config services:web:build:context: /home/shuchang/docker/docker-compose_example/extends_exampleports:- published: 8000target: 8000volumes:- /data version: '3.9'

通過上述內容可以看到extends確實能達到引用共享配置的作用,但是不推薦在共享配置中定義volume和depend_on配置項,存在服務之間的引用錯亂,以及數據卷內容變更影響服務運行的問題。

總結

以上是生活随笔為你收集整理的docker-compose之环境配置以及服务编排文件的使用讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。