docker-compose管理daocker
Docker-compose管理docker服務
1、安裝docker-compose
@首先確保服務器上已經安裝docker環境,如果沒有安裝使用如下命令進行安裝:
# yum -y install dock
#curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
2、創建docker-compose運行需要的配置文件,
# cat docker-compose.yml
version: ‘2‘
services:
elasticsearch:
build: elasticsearch/
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: "-Xms30g -Xmx30g"
networks:
- docker_elk
volumes:
- ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /data/elasticsearch:/usr/share/elasticsearch/data
logstash:
build: logstash/
command: -f /etc/logstash/conf.d/ --config.reload.automatic
volumes:
- ./logstash/config:/etc/logstash/conf.d
ports:
- "5000:5000"
- "5043:5043"
networks:
- docker_elk
depends_on:
- elasticsearch
kibana:
build: kibana/
volumes:
- /tmp/:/etc/archives/
- ./kibana/config/:/etc/kibana/
ports:
- "5601:5601"
networks:
- docker_elk
depends_on:
- elasticsearch
networks:
docker_elk:
driver: bridge
@這個配置文件使用解讀如下:
services:
部署的服務名稱:
build: 填寫服務的Dockerfile路徑
ports:
- “本機port:容器port”
- “………”
- …………….
註:當使用 HOST:CONTAINER 格式來映射端口時,如果你使用的容器端口小於 60 你可能會得到錯誤得結果,因為 YAML 將會解析 xx:yy 這種數字格式為 60 進制。所以建議采用字符串格式。
environment: 設置環境變量
ES_JAVA_OPTS: "-Xms30g -Xmx30g"
networks: 定義docker網絡,名稱一樣的可以互通。
- “docker_elk”
volumes: 掛載本地目錄到容器
- /data:/data
command: -f /etc/logstash/conf.d/ --config.reload.automatic
command用於覆蓋容器後執行的命令
networks:
docker_elk:
driver:bridge
extends: 基於已有的服務進行擴展
file: common.yml
使用這種方式管理docker容器,前提必須已經創建好了Dockerfile,在相應的目錄下。
@第二種格式是docker-compose自己pull鏡像,不用創建Dockerfile
# cat docker-compose.yml
version: ‘2‘
services:
App1:
image: nginx #指定為鏡像名稱或鏡像 ID。如果鏡像在本地不存在,Compose 將會嘗試拉去這個鏡像。
ports:
- "8080:80"
networks:
- "netName1"
volumes:
- /opt/conf/:/mnt
App2:
image: nginx
ports:
- "8081:80"
networks:
- "netName1"
volumes:
- /opt/conf/nginx.conf:/etc/nginx/nginx.conf
App3:
image: nginx
- /opt/web/:/usr/share/nginx/html
ports:
- "8082:80"
networks:
- "netName2"
networks:
netName1:
driver: bridge
netName2:
driver: bridge
啟動服務:
# docker-compose up -d
# docker-compose ps
# docker-compose exec App1 bash
# docker-compose restart App1
# docker-compose stop App1
# docker-compose stop
# docker-compose start
docker-compose管理daocker