mysql 设置UTF-8编码
https://github.com/docker-library/mysql/issues/131#issuecomment-806339446
同一网络无法使用localhost 或 127.0.0.1连接
localhost总是指当前的容器,如果需要获取其他容器,需要使用该容器的compose 服务名。
如:
version: "2"services:prometheus:container_name: prometheusimage: prom/prometheus:latestvolumes:- ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.ymlports:- "9090:9090"restart: on-failurenode_exporter:image: quay.io/prometheus/node-exporter:latestcontainer_name: node_exportercommand:- '--path.rootfs=/host'pid: hostrestart: unless-stoppedvolumes:- '/:/host:ro,rslave'
prometheus需要获取node_exporter的数据,node_exporter数据可以通过命令 curl http://localhost:9100/metrics 获取。
在Prometheus配置文件中不能用 localhost ,因为localhost指prometheus这个服务,而不是宿主机,因此需要使用 node_exporter 。这个 node_exporter 来自服务 node_exporter ,因为compose中服务名就是hostname。
等待某容器彻底启动后再启动其他容器
使用Docker部署时,对于一些容器,它们通常需要等待某一容器彻能够接受外部通讯时(ready状态)才能启动。对于这种情况,docker-compose提供了 depends_on 来解决,但 depends_on 只会在被依赖的容器启动后(start状态)就启动依赖容器。想要等待被依赖容器处于ready时再启动容器,那就需要 healthcheck 。
通过 healthcheck ,当容器能够通过 test 测试后,就会被标记为 healthy 。依赖容器通过 condition 的 service_healthy 判断被依赖容器的状态,当被依赖的容器是 healthy 时,就能启动依赖容器了。
场景举例
如果有一个数据库容器db要启动,一个应用容器 web 也要启动,但 web 需要等待 db 能够正常进行数据库通信才可以启动,否则就会报错:无法连接数据库。
version: "3"services:db:container_name: dbimage: db/latesthealthcheck:test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]timeout: 20sretries: 10web:container_name: webimage: web/latestdepends_on:db:condition: service_healthy
