DOCKER學習_017:Docker-Compose介紹

dockers三駕馬車html

  •  Docker Machine
  •  Docker Swarm
  •  Docker Compose

一 Docker Compose介紹

Docker Compose是一個定義和運行多容器應用的單機編排工具。經過Docker Compose你可使用一個單一的YAML文件來配置多個應用服務,經過一條命令,就能夠將全部配置的服務所有啓動起來。git

1.1 使用Docker Compose的三個步驟

  • 使用Dockerfile定義環境,這樣能夠確保其在任意地方運行github

  • 使用docker-compose.yml文件定義服務,這樣它們就能夠在獨立環境中一塊兒運行redis

  • 運行docker-compose up使用docker-compose啓動全部應用docker

1.2 Docker Compose能夠管理應用的整個生命週期

  • 啓動、中止、重建服務apache

  • 查看服務的運行狀態vim

  • 流式輸出服務日誌bash

  • 對服務執行一次性命令curl

二 Docker Compose安裝使用

2.1 二進制安裝

下載地址:https://github.com/docker/compose/releasestcp

 

 

同時還有安裝方式,也能夠wget下載上面的文件,自己也是一個二進制文件

 [root@docker-server3 ~]# curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    470      0 --:--:--  0:00:01 --:--:--   470
100 16.2M  100 16.2M    0     0   767k      0  0:00:21  0:00:21 --:--:-- 1622k

[root@docker-server3 ~]# chmod +x /usr/local/bin/docker-compose

[root@docker-server3 ~]# docker-compose --version

docker-compose version 1.25.0, build 0a186604

[root@docker-server3 ~]# mkdir /docker-compose

[root@docker-server3 ~]# cd /docker-compose

2.2 簡單使用

寫一個簡單的dockerpose文件

[root@docker-server3 docker-compose]# vim docker-compose.yml

version: '3'
services:
  httpd-test:
    image: httpd:2.4

[root@docker-server3 docker-compose]# docker-compose up -d

Creating network "docker-compose_default" with the default driver
Pulling httpd-test (httpd:2.4)...
2.4: Pulling from library/httpd
8ec398bc0356: Already exists
354e6904d655: Pull complete
27298e4c749a: Pull complete
10e27104ba69: Pull complete
36412f6b2f6e: Pull complete
Digest: sha256:769018135ba22d3a7a2b91cb89b8de711562cdf51ad6621b2b9b13e95f3798de
Status: Downloaded newer image for httpd:2.4
Creating docker-compose_httpd-test_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
06a7e97739b2        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        80/tcp              docker-compose_httpd-test_1

刪除而後在執行,就會拉去一個同樣的鏡像

[root@docker-server3 docker-compose]# docker rm -f 06a7e97739b2

[root@docker-server3 docker-compose]# docker-compose up -d

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
d184230f2de6        httpd:2.4           "httpd-foreground"   3 seconds ago       Up 2 seconds        80/tcp              docker-compose_httpd-test_1

同時建立兩個容器,修改compose文件

[root@docker-server3 docker-compose]# vi docker-compose.yml

version: '3'
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]#  cat /data/index.html

just a test

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                NAMES
8b60d28de874        httpd:2.4           "httpd-foreground"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   6 minutes ago       Up 6 minutes        80/tcp               docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# docker exec -it 8b60d28de874 /bin/bash

root@8b60d28de874:/usr/local/apache2# ls /var/www/html/
ca_download  database  index.html  job_logs  psc  redis  registry  secret
root@8b60d28de874:/usr/local/apache2# cat /var/www/html/index.html 
just a test

[root@docker-server3 docker-compose]# vi docker-compose.yml 

version: '3'
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs"
    ports:
      - "8080:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
docker-compose_httpd-test-2_1 is up-to-date
Creating docker-compose_httpd-test-3_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
a3d24a4c5d25        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1
8b60d28de874        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   14 minutes ago      Up 14 minutes       80/tcp                 docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080

just a test

[root@docker-server3 docker-compose]# echo 111 > /data/111.html

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080/111.html

111

以上就是docker的一個簡答的操做

2.3 docker-compose的經常使用指令

在上面的一個簡單示例中,咱們已經使用了docker-compose up來啓動一個docker-compose.yml文件定義的服務。剛剛經過docker-compose up雖然啓動了服務,當是docker-compose指令卻在前臺執行,若是須要將其放入後臺運行,可使用-d參數:

docker-compose up -d      #後臺運行
docker-compose -f xx.yaml up -d    #若是yml文件不是docker-compose.yml須要使用-f強制指定
docker-compose down     #刪除容器
docker-compose ps        #做用也是查看容器,可是它只能查看docker-compose啓動的容器,使用docker run啓動的容器沒法看到
docker-compose start
docker-compose stop
docker-compose restart 
docker-compose logs

[root@docker-server3 docker-compose]# docker-compose down

Stopping docker-compose_httpd-test-3_1 ... done
Stopping docker-compose_httpd-test-2_1 ... done
Stopping docker-compose_httpd-test_1   ... done
Removing docker-compose_httpd-test-3_1 ... done
Removing docker-compose_httpd-test-2_1 ... done
Removing docker-compose_httpd-test_1   ... done
Removing network docker-compose_default

[root@docker-server3 docker-compose]# docker-compose up -d 

Creating network "docker-compose_default" with the default driver
Creating docker-compose_httpd-test-3_1 ... done
Creating docker-compose_httpd-test_1   ... done
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
ddf6b2a8f4bb        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
a1dabe398019        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        80/tcp                 docker-compose_httpd-test_1
81030fea5437        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1

[root@docker-server3 docker-compose]# docker-compose ps -a

            Name                    Command        State          Ports        
-------------------------------------------------------------------------------
docker-compose_httpd-test-2_1   httpd-foreground   Up      0.0.0.0:80->80/tcp  
docker-compose_httpd-test-3_1   httpd-foreground   Up      0.0.0.0:8080->80/tcp
docker-compose_httpd-test_1     httpd-foreground   Up      80/tcp

啓動中止的操做

前面就是對docker-compose的一個簡單介紹的操做,後續在仔細研究docker-compose的語法以及案例


博主聲明:本文的內容來源主要來自譽天教育晏威老師,由本人實驗完成操做驗證,須要的博友請聯繫譽天教育(http://www.yutianedu.com/),得到官方贊成或者晏老師(https://www.cnblogs.com/breezey/)本人贊成便可轉載,謝謝!

相關文章
相關標籤/搜索