docker-compose之环境配置以及服务编排文件的使用讲解
本文主要講解以下兩塊內(nèi)容:環(huán)境配置的作用及常規(guī)使用、服務(wù)配置文件的解析以及常規(guī)使用
必備知識(shí):
在講解服務(wù)配置文件和環(huán)境配置文件之前,首先要對(duì)docker以及編排工具compose有一定的了解,然后,才能結(jié)合服務(wù)配置文件編排整個(gè)項(xiàng)目的服務(wù)以及其依賴關(guān)系等。
環(huán)境變量:在容器中生效的全局變量值
環(huán)境配置文件:可以替換compose服務(wù)配置文件中的屬性變量。compose默認(rèn)讀取環(huán)境配置文件為“.env”,也可以通過(guò)--env-file指定相應(yīng)的配置文件
compose file:服務(wù)編排文件,主要定義了一系列服務(wù)的配置信息以及依賴等,主要包括:端口、鏡像、環(huán)境信息、依賴關(guān)系等
注意:此處的環(huán)境變量和.env類型的環(huán)境配置文件是兩個(gè)概念。環(huán)境變量主要是在compose部署啟動(dòng)服務(wù)容器后,在容器中生效的變量;而.env中配置的變量值,主要是在定義服務(wù)配置文件時(shí),去替換相應(yīng)的屬性。二者的作用范圍不同,前者是服務(wù)啟動(dòng)后,在容器中作用的環(huán)境變量,后者是為了方便定義服務(wù)配置文件而構(gòu)建變量文件。
在Compose文件中替換環(huán)境變量(.env)
在編輯Compose file時(shí),可以通過(guò)環(huán)境變量的形式來(lái)填充變量的值
web:image: "webapp:${TAG}"那么如何傳入這些環(huán)境變量呢?主要是通過(guò)創(chuàng)建.env配置文件并定義相關(guān)屬性,然后通過(guò)--env-file來(lái)指定我們定義的.env配置文件。
.env使用介紹
1.創(chuàng)建項(xiàng)目目錄
mkdir env_test cd env_test2.創(chuàng)建編排配置文件docker-compose.yml
vi docker-compose.yml#內(nèi)容如下 services:webapp:image: '${image}:${tag}'在該配置文件中,我們將image鏡像使用${image}:${tag}替換成對(duì)應(yīng)的變量值
3.創(chuàng)建.env
vi .env#內(nèi)容如下 image=training/webapp tag=latest注意:compose默認(rèn)會(huì)從當(dāng)前項(xiàng)目路徑加載.env配置文件,如果.env在其他目錄的話,需要通過(guò)--env-file=/path/.env的方式去加載;同樣的,如果配置文件使用其他命名方式的話,也需要使用--env-file=/path/.env_rename來(lái)加載指定配置文件
4.通過(guò)docker-compose config來(lái)校驗(yàn)編排配置文件
#默認(rèn)讀取當(dāng)前項(xiàng)目路徑的.env [shuchang@docker01 env_test]$ docker-compose config services:webapp:image: training/webapp:latest version: '3.9'#讀取指定配置文件.env_case2 #.env_case2內(nèi)容如下 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'可以看到,我們?cè)诃h(huán)境配置文件中定義的變量會(huì)作為environment配置項(xiàng)的內(nèi)容加載到編排配置文件中;同時(shí),通過(guò)--env-file指定環(huán)境配置文件時(shí),同樣能實(shí)現(xiàn)變量的賦值替換。
#在docker-compose.yml中追加ports配置項(xiàng) services:webapp:image: '${image}:${tag}'ports:- '${host_port}:${container_port}'#并在.env中配置對(duì)應(yīng)的變量 host_port=8080 container_port=80#由于.env_case2中并未定義兩個(gè)變量,會(huì)拋出異常 [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配置項(xiàng)后 [shuchang@docker01 env_test]$ docker-compose --env-file .env_case2 config services:webapp:image: test:1.1 version: '3.9'在容器中使用環(huán)境變量
容器中設(shè)置變量,主要是通過(guò)在docker-compose.yml中對(duì)應(yīng)的服務(wù)下配置environment項(xiàng),效果等同于docker-compose run -e variable=value
web:environment:- DEBUG=1同時(shí),還可以在配置env_file配置項(xiàng),從指定的配置文件中加載環(huán)境變量
web:env_file:- web-variables.env?首先,我們配置docker-compose.yml,在webapp服務(wù)下配置env_file配置項(xiàng),加載文件中的環(huán)境變量到webapp服務(wù)容器內(nèi)
vi docker-compose.yml #配置內(nèi)容如下 services:webapp:image: '${image}:${tag}'env_file:- './webapp.env'?然后定義配置文件webapp.env
vi webapp.env #內(nèi)容如下 image=training/webapp tag=latest host_port=8080 container_port=80最后通過(guò)docker-compose config來(lái)檢驗(yàn)配置并輸出,可以看到在webapp.env中定義的屬性都合并到編排文件的environment配置項(xiàng)中了
[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進(jìn)行服務(wù)編排部署的配置文件,在整個(gè)過(guò)程中起到了決定性的作用。它可以定義服務(wù)的一些基本信息,例如:端口、鏡像、數(shù)據(jù)卷等等;同時(shí),還能聲明服務(wù)之間的依賴關(guān)系,決定服務(wù)的啟動(dòng)順序等。因此,了解compose file的常規(guī)配置項(xiàng)是十分有必要的!
compose 默認(rèn)是直接加載docker-compose.yml作為服務(wù)的配置文件的,如需指定其他配置文件,需要通過(guò)-f /path/docker-compose-customerized.yml來(lái)加載配置文件。
Compose file配置項(xiàng)解析
見(jiàn)https://blog.csdn.net/weixin_43762303/article/details/123397714
?profiles配置規(guī)劃服務(wù)
配置文件允許通過(guò)profiles配置項(xiàng)選擇性地啟用服務(wù)來(lái)針對(duì)各種用途和環(huán)境調(diào)整 Compose 應(yīng)用程序模型。
version: "3.9" services:frontend:image: frontendprofiles: ["frontend"]phpmyadmin:image: phpmyadmindepends_on:- dbprofiles:- debugbackend:image: ibmcom/backenddb:image: mysql當(dāng)前服務(wù)配置文件中,frontend和phpmyadmin分別綁定了profiles['frontend']和profiles['debug'];
沒(méi)有profiles屬性的服務(wù)將始終啟用,即在這種情況下運(yùn)行docker-compose up只會(huì)啟動(dòng)backend和db。如需啟動(dòng)上述兩個(gè)服務(wù),需要通過(guò)--profiles指定相應(yīng)配置
[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'因此,通過(guò)在compose file中配置profiles屬性時(shí),可以配置在多種環(huán)境下生效的服務(wù),方便快捷測(cè)試。
多個(gè)Compose file之間的配置共享
默認(rèn)情況下,Compose 讀取兩個(gè)文件,一個(gè)docker-compose.yml和一個(gè)可選?docker-compose.override.yml文件。
如果兩個(gè)文件中都定義了同樣的服務(wù),則會(huì)將兩個(gè)服務(wù)的配置進(jìn)行合并;若需要加載其他compose file,則需要通過(guò)-f /path/docker-compose-other.yml 指定不同名稱的配置文件
注意:
compose針對(duì)image、command和mem_limit這種單值的配置項(xiàng),會(huì)直接用新值替換舊值
針對(duì)ports、expose、external_links、dns、dns_search和tmpfs這種多值的配置項(xiàng),會(huì)將多個(gè)配置項(xiàng)的值合并到一起,且不會(huì)覆蓋
針對(duì)environment、labels、volumes和devices這幾個(gè)多值的配置項(xiàng),若存在交集的話,會(huì)用新的值替換舊的值,其他值合并到一起
樣例:
分別構(gòu)建兩個(gè)配置文件: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執(zhí)行docker-compose config 校驗(yàn)服務(wù)編排配置并輸出
[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'可以看到,兩個(gè)配置文件中配置的db服務(wù)已經(jīng)合并到了一起,同時(shí)暴露了23306:3306,13306:3306端口。因此,可以得知compose默認(rèn)會(huì)讀取docker-compose.yml和docker-compose.override.yml,并將配置合并到一起
但是,如果合并配置后存在沖突的話,就會(huì)出現(xiàn)異常!例如:上述合并端口時(shí),是以主機(jī)的兩個(gè)端口映射到了容器的同一個(gè)端口,這樣是可行的。但是如果是把主機(jī)的同一個(gè)端口映射到容器的兩個(gè)不同的端口呢?那么必然會(huì)導(dǎo)致端口沖突的問(wèn)題,無(wú)法正常啟動(dòng)!
這里我們修改docker-compose.override.yml文件中的db服務(wù),將ports配置項(xiàng)改為23306:3307,這樣就能復(fù)現(xiàn)上述的問(wèn)題
[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.通過(guò)-f <filename>指定compose file
在使用-f指定compose file時(shí),需注意如果只指定一個(gè)文件時(shí),compose只會(huì)讀取指定的配置文件,如docker-compose -f docker-compose.origin.yml config,只會(huì)加載docker-compose.origin.yml一個(gè)文件中定義的服務(wù);而docker-compose -f docker-compose.yml -f docker-compose.origin.yml config 則會(huì)將兩個(gè)配置文件中的服務(wù)進(jìn)行合并。
[shuchang@docker01 case1]$ cat docker-compose.origin.yml #compose針對(duì)image、command和mem_limit這種單值的配置項(xiàng),會(huì)直接用新值替換舊值 #針對(duì)ports、expose、external_links、dns、dns_search和tmpfs這種多值的配置項(xiàng),會(huì)將多個(gè)配置項(xiàng)的值合并到一起,且不會(huì)覆蓋 #針對(duì)environment、labels、volumes和devices這幾個(gè)多值的配置項(xiàng),若存在交集的話,會(huì)用新的值替換舊的值,其他值合并到一起 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'服務(wù)擴(kuò)展之extends
Docker Compose 的extends關(guān)鍵字可以在不同的文件甚至完全不同的項(xiàng)目之間共享通用配置。如果存在多個(gè)重用通用配置選項(xiàng)的服務(wù),那么extends能發(fā)揮關(guān)鍵作用。使用extends您可以在一個(gè)地方定義一組通用的服務(wù)選項(xiàng),并從任何地方引用它。
注意:volume_from 和 depend_on 無(wú)法通過(guò)extends進(jìn)行共享配置,這樣會(huì)導(dǎo)致依賴之間的引用沖突;且volume在本地文件定義的話,能夠確保各個(gè)服務(wù)對(duì)于數(shù)據(jù)卷的內(nèi)容進(jìn)行更改后,不會(huì)影響其他服務(wù)的運(yùn)作。
extends的使用
首先創(chuàng)建compose file,通過(guò)extends引用共享配置
vi docker-compose.yml #內(nèi)容如下 services:web:extends:file: common-services.ymlservice: webapp上述文件中通過(guò)extends引用共享配置common-services.yml中的webapp服務(wù),因此這里需要?jiǎng)?chuàng)建一個(gè)common-services配置文件,并構(gòu)建共享服務(wù)webapp
vi common-services.yml #內(nèi)容如下 services:webapp:build: .ports:- "8000:8000"volumes:- "/data"然后,我們通過(guò)docker-compose config 校驗(yàn)服務(wù)配置并輸出相應(yīng)內(nèi)容
[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'通過(guò)上述內(nèi)容可以看到extends確實(shí)能達(dá)到引用共享配置的作用,但是不推薦在共享配置中定義volume和depend_on配置項(xiàng),存在服務(wù)之間的引用錯(cuò)亂,以及數(shù)據(jù)卷內(nèi)容變更影響服務(wù)運(yùn)行的問(wèn)題。
總結(jié)
以上是生活随笔為你收集整理的docker-compose之环境配置以及服务编排文件的使用讲解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 事件统计_mysql事件统计
- 下一篇: 刷百度下拉那个词与百度快排点击原理是什么