version: '3'
services:
db:
image: "mysql:5.7"
volumes:
- data-mysql:/var/lib/mysql
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
data-mysql:
driver: local
谢谢
曼尼
最佳答案
不需要环境文件。如果仅在docker compose environment
定义中使用变量名,则其值将自动从主机传输到容器:
services:
db:
image: "mysql:5.7"
ports: ['3306:3306']
environment:
MYSQL_ROOT_PASSWORD:
从documentation:
Environment variables with only a key are resolved to their values on the machine Compose is running on, which can be helpful for secret or host-specific values.
如果由于某种原因您想要或需要使用一个环境文件,您还可以:
services:
db:
image: "mysql:5.7"
ports: ['3306:3306']
env_file:
- production.env
并在
production.env
文件(或任何您命名的文件)中,只需放入key=value
行:MYSQL_ROOT_PASSWORD=7op-s3cr37
另请参见The “env_file” configuration option。
关于mysql - 如何使用env_file在docker-compose中隐藏Mysql密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61286800/