MySQL-Docker撰写Healthcheck使用环境变量
作者:互联网
这是我的docker-compose.yml:
version: "3"
services:
mongodb:
image: mongo:4.1
volumes:
- ./mongodb_data:/data/db
container_name: mongodb
ports:
- 27017:27017
mysql:
image: mysql:5.7
#volumes:
#- ./mysql_data:/var/lib/mysql
container_name: mysqldb
ports:
- 3306:3306
- 33060:33060
expose:
- 3306
- 33060
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=somedb
- MYSQL_USER=rstudio
- MYSQL_PASSWORD=password
healthcheck:
test: ["CMD", "mysql", "--user=$MYSQL_USER", "--password=$MYSQL_PASSWORD", "-e", "'SHOW DATABASES;'"]
timeout: 5s
retries: 5
如何使用环境变量MYSQL_USER和MYSQL_PASSWORD?在当前设置中,运行docker compose时收到以下警告:
WARNING: The MYSQL_USER variable is not set. Defaulting to a blank string.
WARNING: The MYSQL_PASSWORD variable is not set. Defaulting to a blank string.
解决方法:
您需要使用双美元符号($$)来告诉docker-compose不要解析这些环境变量.当我尝试使测试生效时,也要更改定义测试的方式,但是我不能不更改它.以下应该可以正常工作:
test: mysql --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD -e 'SHOW DATABASES;'
docker ps结果:最多35秒(正常)
标签:docker,docker-compose,environment-variables,mysql 来源: https://codeday.me/bug/20191108/2006335.html