docker 容器安装conposer_docker学习笔记(二)docker-composer
一、簡介
Compose 項目是 Docker 官方的開源項目,負責實現(xiàn)對 Docker 容器集群的快速編排。
Compose 中有兩個重要的概念:
服務(wù) (service):一個應用的容器,實際上可以包括若干運行相同鏡像的容器實例。
項目 (project):由一組關(guān)聯(lián)的應用容器組成的一個完整業(yè)務(wù)單元,在 docker-compose.yml 文件中定義。
二、安裝
Docker Desktop for Mac/Windows 自帶 docker-compose 二進制文件,安裝 Docker 之后可以直接使用。
$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01
1
2
3
$docker-compose--version
docker-composeversion1.23.2,build1110ad01
三、composer模板文件
編寫一個最簡單的模板
# docker-compose.yml
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
# docker-compose.yml
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
version 表示模板使用的規(guī)則版本號
services表示所有服務(wù)
webapp表示服務(wù)名稱
image表示鏡像,nginx:v3是我們之前構(gòu)建的鏡像
ports表示端口映射
啟動服務(wù)
$ docker-compose up -d
1
2
$docker-composeup-d
-d 表示后臺常駐進程方式運行
下面我們挑選幾個進行演練。
build
指定 Dockerfile 所在文件夾的路徑(可以是絕對路徑,或者相對 docker-compose.yml 文件的路徑)。 Compose 將會利用它自動構(gòu)建這個鏡像,然后使用這個鏡像。
version: '3'
services:
webapp:
build: ./dir
1
2
3
4
5
6
version:'3'
services:
webapp:
build:./dir
也可以使用 context 指令指定 Dockerfile 所在文件夾的路徑。
使用 dockerfile 指令指定 Dockerfile 文件名。
使用 arg 指令指定構(gòu)建鏡像時的變量。
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
1
2
3
4
5
6
7
8
9
10
version:'3'
services:
webapp:
build:
context:./dir
dockerfile:Dockerfile-alternate
args:
buildno:1
container_name
指定容器名稱。默認將會使用 項目名稱_服務(wù)名稱_序號
version: "3"
services:
webapp:
container_name: my-ngxin
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
version:"3"
services:
webapp:
container_name:my-ngxin
image:nginx:v3
ports:
-"80:80"
查看服務(wù)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19f8fdb1f76d nginx:v3 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp my-ngxin
1
2
3
4
5
$dockerps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
19f8fdb1f76dnginx:v3"nginx -g 'daemon of…"4secondsagoUp3seconds0.0.0.0:80->80/tcpmy-ngxin
depends_on
解決容器的依賴、啟動先后的問題。下面為例,會先啟動redis,之后再啟動webapp。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
depends_on:
- redis
redis:
image: redis:latest
1
2
3
4
5
6
7
8
9
10
11
12
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
depends_on:
-redis
redis:
image:redis:latest
env_file
從文件中獲取環(huán)境變量,可以為單獨的文件路徑或列表。
如果通過 docker-compose -f FILE 方式來指定 Compose 模板文件,則 env_file 中變量的路徑會基于模板文件路徑。默認為.env模板。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
env_file: .env
redis:
image: redis:latest
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
env_file:.env
redis:
image:redis:latest
env_file:
-./common.env
-./apps/web.env
-/opt/secrets.env
environment
設(shè)置環(huán)境變量。你可以使用數(shù)組或字典兩種格式。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
environment:
RACK_ENV: development
SESSION_SECRET:
1
2
3
4
5
6
7
8
9
10
11
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
environment:
RACK_ENV:development
SESSION_SECRET:
如果變量名稱或者值中用到 true|false,yes|no 等表達 布爾 含義的詞匯,最好放到引號里,避免 YAML 自動解析某些內(nèi)容為對應的布爾語義。這些特定詞匯,包括
y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
1
2
y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
expose
暴露端口,但不映射到宿主機,只被連接的服務(wù)訪問。
僅可以指定內(nèi)部端口為參數(shù)
version: "3"
services:
webapp:
image: nginx:v3
expose:
- "3000"
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
10
version:"3"
services:
webapp:
image:nginx:v3
expose:
-"3000"
ports:
-"80:80"
network_mode
設(shè)置網(wǎng)絡(luò)模式。使用和 docker run 的 --network 參數(shù)一樣的值。
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
1
2
3
4
5
6
network_mode:"bridge"
network_mode:"host"
network_mode:"none"
network_mode:"service:[service name]"
network_mode:"container:[container name/id]"
networks
配置容器連接的網(wǎng)絡(luò)。
假設(shè),在目錄app下創(chuàng)建docker-compose.yml,內(nèi)容如下:
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
使用docker-compose up啟動容器后,這些容器都會被加入app_default網(wǎng)絡(luò)中。使用docker network ls可以查看網(wǎng)絡(luò)列表,docker network inspect可以查看對應網(wǎng)絡(luò)的配置。
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
0a9fa5ffdf97 app_default bridge local
...
1
2
3
4
5
$dockernetworkls
NETWORKIDNAMEDRIVERSCOPE
0a9fa5ffdf97app_defaultbridgelocal
...
假設(shè)我們lnmp環(huán)境需要網(wǎng)絡(luò)隔離,如下配置,nginx、mysql、redis都有獨立的網(wǎng)絡(luò),相互隔離,php需要鏈接他們,則都可以所有網(wǎng)絡(luò)都可以使用。
version: '3'
services:
nginx:
image: nginx:alpine
# ...
networks:
- net-php
php:
# ...
networks:
- net-php
- net-mysql
- net-redis
mysql:
image: mysql:5.7
# ...
networks:
- net-mysql
redis:
image: redis:latest
# ..
networks:
- net-redis
networks:
net-php:
net-mysql:
net-redis:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
version:'3'
services:
nginx:
image:nginx:alpine
# ...
networks:
-net-php
php:
# ...
networks:
-net-php
-net-mysql
-net-redis
mysql:
image:mysql:5.7
# ...
networks:
-net-mysql
redis:
image:redis:latest
# ..
networks:
-net-redis
networks:
net-php:
net-mysql:
net-redis:
ports
暴露端口信息。
使用宿主端口:容器端口 (HOST:CONTAINER) 格式,或者僅僅指定容器的端口(宿主將會隨機選擇端口)都可以。
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
1
2
3
4
5
6
7
ports:
-"3000"
-"8000:8000"
-"49100:22"
-"127.0.0.1:8001:8001"
注意:當使用 HOST:CONTAINER 格式來映射端口時,如果你使用的容器端口小于 60 并且沒放到引號里,可能會得到錯誤結(jié)果,因為 YAML 會自動解析 xx:yy 這種數(shù)字格式為 60 進制。為避免出現(xiàn)這種問題,建議數(shù)字串都采用引號包括起來的字符串格式。
secrets
存儲敏感數(shù)據(jù),例如 mysql 服務(wù)密碼。
version: "3.1"
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
secrets:
- db_root_password
- my_other_secret
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version:"3.1"
services:
mysql:
image:mysql
environment:
MYSQL_ROOT_PASSWORD_FILE:/run/secrets/db_root_password
secrets:
-db_root_password
-my_other_secret
secrets:
my_secret:
file:./my_secret.txt
my_other_secret:
external:true
volumes
數(shù)據(jù)卷所掛載路徑設(shè)置。可以設(shè)置為宿主機路徑(HOST:CONTAINER)或者數(shù)據(jù)卷名稱(VOLUME:CONTAINER),并且可以設(shè)置訪問模式 (HOST:CONTAINER:ro)。
該指令中路徑支持相對路徑。
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
1
2
3
4
5
volumes:
-/var/lib/mysql
-cache/:/tmp/cache
-~/configs:/etc/configs/:ro
如果路徑為數(shù)據(jù)卷名稱,必須在文件中配置數(shù)據(jù)卷。
version: "3"
services:
my_src:
image: mysql:8.0
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
1
2
3
4
5
6
7
8
9
10
11
version:"3"
services:
my_src:
image:mysql:8.0
volumes:
-mysql_data:/var/lib/mysql
volumes:
mysql_data:
本文參考地址:https://yeasy.gitbooks.io/docker_practice/compose/compose_file.html
總結(jié)
以上是生活随笔為你收集整理的docker 容器安装conposer_docker学习笔记(二)docker-composer的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图片不能置于底层怎么办_PPT中常遇到的
- 下一篇: wince车机可以连接电脑吗_揭秘低价电