Docker系列之十:Docker Compose的使用
系列連結
Docker系列之一:Docker介紹及在Ubuntu上安裝
Docker系列之五:Volume 卷的使用——以Redis為例
Docker系列之六:Volume 卷的使用——在Dockerfile中的用法
Docker系列之七:Docker 網路(內部網路、容器之間的連線)
Docker系列之八:在Dockerfile中使用多段構建Muti-stage build
Docker系列之九:Docker用於持續整合,構建Jenkins和Docker伺服器
簡介
在之前執行一個容器必須使用一段命令如:docker run -itd -p 80:80 -v /var/data/:/var/data/ --volume-from cong1 --net congnet --name mynginx nginx,每次執行都需要寫這麼一段,有點煩,接下來我們使用Docker compose來解決這個問題, 其實run 後面的都算配置,而yaml檔案是目前寫配置檔案的利器。
使用Docker compose,可以用一個YAML檔案定義一組要啟用的容器,以及容器執行的屬性,Docker compose稱這些容器為“服務”。
安裝Docker Compose
下載最新的docker-compose,並安裝到資料夾/usr/local/bin/docker-compose 下。
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改許可權,讓它變成得可執行
sudo chmod +x /usr/local/bin/docker-compose
檢查版本,出現如下介面,說明安裝成功了
[email protected]:/home/cong/DockerDemo# docker-compose -v
docker-compose version 1.23.1, build b02f1306
使用Docker Compose建立一個image,並執行
建立一個資料夾,像下面這樣。
[email protected]:/home/cong/DockerDemo# ls
docker-compose.yml Dockerfile index.html
[email protected]:/home/cong/DockerDemo# cat Dockerfile
FROM nginx
WORKDIR /usr/share/nginx/html
COPY . .
#Volume /home/app
EXPOSE 80
[email protected]:/home/cong/DockerDemo# cat docker-compose.yml
---
version: '3'
services:
web:
build: "/home/cong/DockerDemo/"
ports:
- "5000:80"
redis:
image: "redis"
[email protected]:/home/cong/DockerDemo# cat index.html
<h1>Hello world</h1>
<h2>my greet</h2>
從上可以看出裡面有兩個服務:web 和 redis, 執行如下命令進行構建docker image.
[email protected]:/home/cong/DockerDemo# docker-compose build
redis uses an image, skipping
Building web
Step 1/4 : FROM nginx
---> dbfc48660aeb
Step 2/4 : WORKDIR /usr/share/nginx/html
---> Using cache
---> 49eb0195ae40
Step 3/4 : COPY . .
---> 45a1d6022ef6
Step 4/4 : EXPOSE 80
---> Running in e18fcf899aff
Removing intermediate container e18fcf899aff
---> 36f9a746361b
Successfully built 36f9a746361b
Successfully tagged dockerdemo_web:latest
這裡由於redis服務中使用的是的redis映象,這裡顯示為跳過,只生成一個映象,這個映象的名字是以你的當前資料夾名+服務名命名的,生成出來的映象如下:
[email protected]:/home/cong/DockerDemo# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerdemo_web latest 36f9a746361b 2 minutes ago 109MB
執行如下命令,執行容器,-d 表示detached在後臺執行。
[email protected]:/home/cong/DockerDemo# docker-compose up -d
Starting dockerdemo_web_1_4b7b83909e42 ... done
Starting dockerdemo_redis_1_dfd3e5c242ff ... done
[email protected]:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da2cb8d44ee5 dockerdemo_web "nginx -g 'daemon of…" 21 seconds ago Up 3 seconds 0.0.0.0:5000->80/tcp dockerdemo_web_1_4b7b83909e42
a8b63ebd622d redis "docker-entrypoint.s…" 21 seconds ago Up 2 seconds 6379/tcp dockerdemo_redis_1_dfd3e5c242ff
[email protected]:/home/cong/DockerDemo# docker network ls
NETWORK ID NAME DRIVER SCOPE
0a5324c3bd83 bridge bridge local
f04d01cfd231 dockerdemo_default bridge local
fa685cfac4f3 host host local
22f6181538a9 none null local
發現這兩容器都執行起來了,而且它們都在網路dockerdemo_default中。
接下來我們修改docker-compose.yml檔案,將當前路徑下的檔案掛載到容器中。yaml檔案內容如下,然後再執行docker -compose up命令,它會rebuild image, 最終看到資料夾掛載成功了。
[email protected]:/home/cong/DockerDemo# cat docker-compose.yml
---
version: '3'
services:
web:
volumes:
- .:/usr/share/nginx/html
build: "/home/cong/DockerDemo/"
ports:
- "5000:80"
redis:
image: "redis"
#修改當前路徑裡index.html檔案的內容
[email protected]:/home/cong/DockerDemo# echo "<h1>hello</h1>" > index.html
#再執行docker-compose up,它會自動更新映象並執行容器
[email protected]:/home/cong/DockerDemo# docker-compose up -d
dockerdemo_redis_1_dfd3e5c242ff is up-to-date
Recreating dockerdemo_web_1_4b7b83909e42 ... done
#檢查是否掛載成功
[email protected]:/home/cong/DockerDemo# curl localhost:5000
<h1>hello</h1>
執行docker-compose ps 看看通過docker-compose 執行起來的容器
[email protected]:/home/cong/DockerDemo# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------
dockerdemo_redis_1_dfd3e5c242ff docker-entrypoint.sh redis ... Up 6379/tcp
dockerdemo_web_1_4b7b83909e42 nginx -g daemon off; Up 0.0.0.0:5000->80/tcp
[email protected]:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0020488cb04 dockerdemo_web "nginx -g 'daemon of…" 17 hours ago Up 17 hours 0.0.0.0:5000->80/tcp dockerdemo_web_1_4b7b83909e42
a8b63ebd622d redis "docker-entrypoint.s…" 17 hours ago Up 17 hours 6379/tcp dockerdemo_redis_1_dfd3e5c242ff
78761e99c06f nginx "nginx -g 'daemon of…" 45 hours ago Up 45 hours 0.0.0.0:80->80/tcp mynginx
1b25aef5f610 redis "docker-entrypoint.s…" 8 days ago Up 8 days 0.0.0.0:6379->6379/tcp kind_curran
如果想要執行某個服務裡命令,或者說執行某個容器裡的命令呢,下面是檢視web服務裡的環境變數
[email protected]:/home/cong/DockerDemo# docker-compose run web env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=5435187b75a1
TERM=xterm
NGINX_VERSION=1.15.5-1~stretch
NJS_VERSION=1.15.5.0.2.4-1~stretch
HOME=/root
使用Docker Compose停止服務
直接看下面的命令
#停止服務
[email protected]:/home/cong/DockerDemo# docker-compose down
Stopping dockerdemo_web_1_4b7b83909e42 ... done
Stopping dockerdemo_redis_1_dfd3e5c242ff ... done
Removing dockerdemo_redis_run_1_636fb680637e ... done
Removing dockerdemo_web_run_1_1e57ca097b87 ... done
Removing dockerdemo_web_1_4b7b83909e42 ... done
Removing dockerdemo_redis_1_dfd3e5c242ff ... done
Removing network dockerdemo_default
[email protected]:/home/cong/DockerDemo# docker-compose ps
Name Command State Ports
------------------------------
#服務停止後,但映象還在
[email protected]:/home/cong/DockerDemo# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerdemo_web latest 36f9a746361b 17 hours ago 109MB
redis latest 1babb1dde7e1 3 weeks ago 94.9MB
node latest a2b9536415c2 4 weeks ago 674MB
nginx latest dbfc48660aeb 4 weeks ago 109MB
#服務停止後,但卷還在
[email protected]:/home/cong/DockerDemo# docker volume ls
DRIVER VOLUME NAME
local 1ff8ca8ce28be5851d217134e90b74cb24c35035284ce93d83363085e37adac4
# 移除卷
[email protected]:/home/cong/DockerDemo# docker-compose down --volumes
Removing network dockerdemo_default
WARNING: Network dockerdemo_default not found.
到這裡就結束, 這裡只演示了使用docker-compose最基本的用法,如深度學習,請參考如下連結。