标签 docker-compose 下的文章

问题:

新建了一个docker-compose.yaml配置,然后启动服务,发现连不上mysql
报错:Access denied for user

分析:

  1. docker-compose.yaml配置如下:

    version: "3.9"
    services:
    mysql:
    image: mysql:5.7
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4 
      --collation-server=utf8mb4_unicode_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
    environment:
      - MYSQL_ROOT_PASSWORD=1234$56
    restart: always
    ports:
      - "13306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
      - mysql-conf:/etc/mysql/conf.d
    
    adminer:
    image: adminer
    container_name: adminer
    restart: always
    ports:
      - 18080:8080
    depends_on:
      - mysql
    volumes:
    mysql-data: {}
    mysql-conf: {}
  2. 进入mysql容器,docker exec -it mysql /bin/bash,env查看MYSQL_ROOT_PASSWORD的变量值,发现是MYSQL_ROOT_PASSWORD=1234
  3. 故障点定位到,environment的MYSQL_ROOT_PASSWORD这种赋值方式异常

解决办法或者注意事项:

  1. 查看官网关于环境变量定义的标准用法
    https://docs.docker.com/compose/compose-file/compose-file-v3/#environment
    https://stackoverflow.com/questions/40619582/how-can-i-escape-a-dollar-sign-in-a-docker-compose-file
  2. 修正docker-compose.yaml配置

    version: "3.9"
    services:
    mysql:
    image: mysql:5.7
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4 
      --collation-server=utf8mb4_unicode_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
    environment:
      - MYSQL_ROOT_PASSWORD=1234$$56
    restart: always
    ports:
      - "13306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
      - mysql-conf:/etc/mysql/conf.d
    
    adminer:
    image: adminer
    container_name: adminer
    restart: always
    ports:
      - 18080:8080
    depends_on:
      - mysql
    volumes:
    mysql-data: {}
    mysql-conf: {}
  3. 登录mysql容器,env校验参数成功

docker-compose-prometheus.yaml

version: "3"
networks:
  monitor:
    driver: bridge
services:
  prometheus-server:
    image: prom/prometheus:latest
    container_name: prometheus-server
    expose:
      - "9090"
    ports:
      - "9090:9090"
    restart: always
    networks:
      - monitor
    ulimits:
      nproc: 65535
      nofile:
        soft: 40000
        hard: 40000
    volumes:
      - "/app/docker_app_data/prometheus_server/prometheus.yml:/etc/prometheus/prometheus.yml"
      - "/app/docker_app_data/prometheus_server/data:/prometheus-data"

  prometheus-alertmanager:
    image: prom/alertmanager:latest
    container_name: prometheus-alertmanager
    depends_on:
      - prometheus-server
    expose:
      - "9093"
    ports:
      - "9093:9093"
    restart: always
    networks:
      - monitor
    ulimits:
      nproc: 65535
      nofile:
        soft: 40000
        hard: 40000
    volumes:
      - "/app/docker_app_data/prometheus_alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml"

  prometheus-node-exporter:
    image: prom/node-exporter:latest
    container_name: prometheus-node-exporter
    depends_on:
      - prometheus-server
    expose:
      - "9100"
    ports:
      - "9100:9100"
    restart: always
    networks:
      - monitor
    ulimits:
      nproc: 65535
      nofile:
        soft: 40000
        hard: 40000

  grafana-server:
    image: grafana/grafana:latest
    container_name: grafana-server
    depends_on:
      - prometheus-server
    expose:
      - "3000"
    ports:
      - "3000:3000"
    restart: always
    networks:
      - monitor
    environment:
      - "GF_SERVER_ROOT_URL=http://grafana.server.name"
      - "GF_SECURITY_ADMIN_PASSWORD=!@#123#@!"
      - "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource"
    ulimits:
      nproc: 65535
      nofile:
        soft: 40000
        hard: 40000
    volumes:
      - "/app/docker_app_data/grafana_server/data:/var/lib/grafana"