docker-compose入门
docker-compose适合本地搭建开发环境,生产环境多用swam或者k8s。
docker-compose:本地启动多个容器,即一个应用要用的python前端、java后端、数据库等,每个功能一个container,docker-compose可以一次启动完成,另外他还可以利用scale选项进行负载均衡。
例子一:docker-compose一键启动wordpress
docker-compose
version: '3' services: wordpress: image: wordpress ports: - 8080:80 environment: WORDPRESS_DB_HOST: mysql WORDPRESS_DB_PASSWORD: root networks: - my-bridge mysql: image: mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress volumes: - mysql-data:/var/lib/mysql networks: - my-bridge volumes: mysql-data: networks: my-bridge: driver: bridge
浏览器访问:http://172.16.241.131:8080
不出意外会出错:
Error establishing a database connection
认证方法错误,mysql8.0以后默认的认证方式改了,所以才会有这样的错误
解决方法
#进入mysql容器 docker-compose exec mysql bash #登陆数据库 mysql -u root -p use mysql; #开启root远程访问权限 grant all on *.* to 'root'@'%'; #修改加密规则 alter user 'root'@'localhost' identified by '123456' password expire never; #更新密码 alter user 'root'@'%' identified with mysql_native_password by '123456'; #刷新权限 flush privileges;
再次刷新浏览器就好了
例子二:使用scale负载均衡
docker-compose
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile environment: REDIS_HOST: redis lb: image: dockercloud/haproxy links: - web ports: - 8080:80 volumes: - /var/run/docker.sock:/var/run/docker.sock
Dockerfile
FROM python:2.7 LABEL maintaner="Peng Xiao xiaoquwl@gmail.com" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 80 CMD [ "python", "app.py" ]
app.py
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=80, debug=True)
进入docker-compose.yml所在文件夹,启动docker-compose
docker-compose up --scale web=3 -d
查看是否进行了容器扩展与负载均衡
[root@localhost scale]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db5de6922ea8 dockercloud/haproxy "/sbin/tini -- docke…" 56 minutes ago Up 56 minutes 443/tcp, 1936/tcp, 0.0.0.0:8080->80/tcp scale_lb_1 7061e5a0a6d6 scale_web "python app.py" 56 minutes ago Up 56 minutes 80/tcp scale_web_3 829a6dc26957 scale_web "python app.py" 56 minutes ago Up 56 minutes 80/tcp scale_web_1 b61dc8fe1846 redis "docker-entrypoint.s…" 56 minutes ago Up 56 minutes 6379/tcp scale_redis_1 9b97567cfcf7 scale_web "python app.py" 56 minutes ago Up 56 minutes 80/tcp scale_web_2 [root@localhost scale]# for i in `seq 10`; do curl 172.16.241.132:8080; done Hello Container World! I have been seen 1 times and my hostname is 829a6dc26957. Hello Container World! I have been seen 2 times and my hostname is 9b97567cfcf7. Hello Container World! I have been seen 3 times and my hostname is 7061e5a0a6d6. Hello Container World! I have been seen 4 times and my hostname is 829a6dc26957. Hello Container World! I have been seen 5 times and my hostname is 9b97567cfcf7. Hello Container World! I have been seen 6 times and my hostname is 7061e5a0a6d6. Hello Container World! I have been seen 7 times and my hostname is 829a6dc26957. Hello Container World! I have been seen 8 times and my hostname is 9b97567cfcf7. Hello Container World! I have been seen 9 times and my hostname is 7061e5a0a6d6. Hello Container World! I have been seen 10 times and my hostname is 829a6dc26957.
例子三:python、java、PGdatabase多模块利用docker-compose一键启动