[root@localhost tmp]# cat app.py import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return 'Hello World! I have been seen {} times.\n'.format(count) if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
[root@localhost tmp]# cat requirements.txt flask redis
[root@localhost tmp]# cat Dockerfile FROM python:3.4-alpine ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD ["python", "app.py"]
[root@localhost tmp]# cat docker-compose.yml version: '2' services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine"
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
本章節介紹如何定義 docker-compose.yml 檔案
首先創建項目目錄
mkdir docker cd docker vim docker-compose.yml
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
將容器中的連接埠暴漏給宿主主機。
ports: - "3000" - "80:80" - "22:22" - "127.0.0.1:8000:8000"
預設 "連接埠:連接埠" 將監聽 127.0.0.1 主機。如果需要將連接埠暴漏出去,格式是"IP:PORT:PORT",IP地址是宿主主機的網絡適配器IP地址。
往/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"
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
編譯當前目錄下的 Dockerfile 使用 build: .
version: '3' services: web: build: . ports: - "5000:5000"
指定鏡像名稱
version: "3.7" services: redis-image: build: context: . dockerfile: Dockerfile args: - node=master image: netkiller/redis:latest container_name: redis restart: always ports: - "6379:6379" networks: - redis privileged: true sysctls: net.core.somaxconn: '511' ulimits: nproc: 65535 nofile: soft: 65535 hard: 65535
docker-compose build redis-image 構建鏡像
neo@MacBook-Pro ~/workspace/docker/docker-compose/redis/cluster % docker-compose build redis-image Building redis-image Step 1/12 : FROM redis:latest ---> a55fbf438dfd Step 2/12 : ARG node ---> Using cache ---> 4deb8fc1e1df Step 3/12 : ENV REDIS_PORT 6379 ---> Using cache ---> 5723ff2fe55c Step 4/12 : COPY redis.conf /etc/redis/redis.conf ---> Using cache ---> daf496f8c342 Step 5/12 : COPY docker-entrypoint.sh /usr/local/bin/ ---> Using cache ---> 600ae3b0c059 Step 6/12 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Using cache ---> 630e3813bc8f Step 7/12 : RUN echo 'Asia/Shanghai' >/etc/timezone ---> Using cache ---> 7d48350d6621 Step 8/12 : RUN echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' > /etc/rc.local ---> Using cache ---> c096dc75da72 Step 9/12 : RUN chmod +rw /etc/redis/redis.conf ---> Using cache ---> 25d8b0ac8893 Step 10/12 : EXPOSE $REDIS_PORT ---> Using cache ---> 99f31a88d2ff Step 11/12 : ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] ---> Using cache ---> ef98f89610ae Step 12/12 : CMD [ "redis-server", "/etc/redis/redis.conf" ] ---> Using cache ---> 095823650068 Successfully built 095823650068 Successfully tagged netkiller/redis:latest neo@MacBook-Pro ~/workspace/docker/docker-compose/redis/cluster % docker images | grep netkiller/redis netkiller/redis latest 095823650068 8 minutes ago 95MB