Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | 云栖社区 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Github | Search | About

166.9. docker-compose - Define and run multi-container applications with Docker.

166.9.1. 安装 docker-compose

166.9.1.1. 使用 pip 安装

			
yum install -y python-pip
pip install docker-compose
			
			

166.9.1.2. OSCM 安装

			
curl -s https://raw.githubusercontent.com/oscm/shell/master/virtualization/docker/docker-compose.sh | bash
			
			

166.9.2. 启动/停止

166.9.2.1. 启动

docker-compose up

			
[root@localhost docker]# docker-compose up
			
			

守护进程

			
docker-compose up -d			
			
			

166.9.2.2. 停止

docker-compose down

			
[root@localhost docker]# docker-compose down
Removing docker_membersrvc_1 ... done		
			
			

166.9.3. 查看进程

docker-compose ps

		
[root@localhost docker]# docker-compose ps 
      Name                     Command               State                                   Ports                                  
-----------------------------------------------------------------------------------------------------------------------------------
test_membersrvc_1   membersrvc                       Up      0.0.0.0:7054->7054/tcp                                                 
test_vp0_1          sh -c sleep 5; peer node s ...   Up      0.0.0.0:7050->7050/tcp, 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp			
		
		

166.9.4. 查看日志

		
docker-compose logs -f vp0		
		
		

166.9.5. 执行命令

		
docker-compose exec vp0 bash		
		
		

166.9.6. docker-compose.yml

本章节介绍如何定义 docker-compose.yml 文件

首先创建项目目录

		
mkdir docker
cd docker
vim 	docker-compose.yml
		
		

166.9.6.1. 版本号

			
version: '3'	
			
			

166.9.6.2. 镜像

image: mysql:5.7 表示使用 mysql:5.7 镜像, image: mysql:latest 表示 mysql 最新版

			
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress			
			
			
166.9.6.2.1. 挂在卷
				
     volumes:
       - db_data:/var/lib/mysql
				
				

166.9.6.3. 映射端口的标签

将容器中的端口暴漏给宿主主机。

			
ports:
 - "3000"
 - "80:80"
 - "22:22"
 - "127.0.0.1:8000:8000"
			
			

默认 "端口:端口" 将监听 127.0.0.1 主机。如果需要将端口暴漏出去,格式是"IP:PORT:PORT",IP地址是宿主主机的网络适配器IP地址。

166.9.6.4. 添加 hosts 文件

往/etc/hosts文件中添加主机名,与Docker client的--add-host类似:

			
	extra_hosts:
		- "orderer.example.com:10.130.116.8"
		- "peer0.org1.example.com:10.130.116.9"
		- "peer1.org1.example.com:10.130.116.10"
		- "peer0.org2.example.com:10.130.116.25"
		- "peer1.org2.example.com:10.130.116.27"
			
			

166.9.6.5. 设置环境变量

environment 实现容器中环境变量的定义

			
version: '3'

networks:
  basic:

services:
  tools:
    container_name: tools 
    image: hyperledger/fabric-tools
    tty: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
      - CORE_CHAINCODE_KEEPALIVE=10
    # working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    working_dir: /root/netkiller
    command: /bin/bash
    volumes:
        - /var/run/:/host/var/run/
        - ~/netkiller:/root/netkiller
        - ./chaincode/:/opt/gopath/src/github.com/
        - ./crypto:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
    networks:
        - basic
			
			

166.9.6.6. 临时文件系统

挂载临时目录到容器:

			
tmpfs: /run
tmpfs:
  - /run
  - /tmp