mysql 设置UTF-8编码

https://github.com/docker-library/mysql/issues/131#issuecomment-806339446

同一网络无法使用localhost 或 127.0.0.1连接

localhost总是指当前的容器,如果需要获取其他容器,需要使用该容器的compose 服务名
如:

  1. version: "2"
  2. services:
  3. prometheus:
  4. container_name: prometheus
  5. image: prom/prometheus:latest
  6. volumes:
  7. - ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
  8. ports:
  9. - "9090:9090"
  10. restart: on-failure
  11. node_exporter:
  12. image: quay.io/prometheus/node-exporter:latest
  13. container_name: node_exporter
  14. command:
  15. - '--path.rootfs=/host'
  16. pid: host
  17. restart: unless-stopped
  18. volumes:
  19. - '/:/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 。依赖容器通过 conditionservice_healthy 判断被依赖容器的状态,当被依赖的容器是 healthy 时,就能启动依赖容器了。

场景举例

如果有一个数据库容器db要启动,一个应用容器 web 也要启动,但 web 需要等待 db 能够正常进行数据库通信才可以启动,否则就会报错:无法连接数据库。

  1. version: "3"
  2. services:
  3. db:
  4. container_name: db
  5. image: db/latest
  6. healthcheck:
  7. test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
  8. timeout: 20s
  9. retries: 10
  10. web:
  11. container_name: web
  12. image: web/latest
  13. depends_on:
  14. db:
  15. condition: service_healthy

参考