关于docker-compose.yaml传递特殊字符的环境变量问题
问题:
新建了一个docker-compose.yaml配置,然后启动服务,发现连不上mysql
报错:Access denied for user
分析:
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: {}
- 进入mysql容器,docker exec -it mysql /bin/bash,env查看MYSQL_ROOT_PASSWORD的变量值,发现是MYSQL_ROOT_PASSWORD=1234
- 故障点定位到,environment的MYSQL_ROOT_PASSWORD这种赋值方式异常
解决办法或者注意事项:
- 查看官网关于环境变量定义的标准用法
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 修正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: {}
- 登录mysql容器,env校验参数成功
评论已关闭