我想使用 centos/mysql-56-centos7 镜像启动一个 mysql 容器。有两种方法:
1)
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-56-centos7
这种方法很好用
2)docker-compose.yml
version: '2'
services:
mysql:
image: centos/mysql-56-centos7:latest
restart: always
container_name: mysql
在 .env 文件中,我设置
MYSQL_ROOT_PASSWORD=123456
但启动总是失败:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b651a67ae0b310a7e08ac20919c08583e50a2dc9f515a16618c4db130ea65415 centos/mysql-56-centos7:latest "container-entrypoint run-mysqld" 55 seconds ago Restarting (1) 25 seconds ago mysql
日志信息
mysql | => sourcing 20-validate-variables.sh ...
mysql | You must either specify the following environment variables:
mysql | MYSQL_USER (regex: '^[a-zA-Z0-9_]+$')
mysql | MYSQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
mysql | MYSQL_DATABASE (regex: '^[a-zA-Z0-9_]+$')
mysql | Or the following environment variable:
mysql | MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
mysql | Or both.
mysql | Optional Settings:
mysql | MYSQL_LOWER_CASE_TABLE_NAMES (default: 0)
mysql | MYSQL_LOG_QUERIES_ENABLED (default: 0)
mysql | MYSQL_MAX_CONNECTIONS (default: 151)
mysql | MYSQL_FT_MIN_WORD_LEN (default: 4)
mysql | MYSQL_FT_MAX_WORD_LEN (default: 20)
mysql | MYSQL_AIO (default: 1)
mysql | MYSQL_KEY_BUFFER_SIZE (default: 32M or 10% of available memory)
mysql | MYSQL_MAX_ALLOWED_PACKET (default: 200M)
mysql | MYSQL_TABLE_OPEN_CACHE (default: 400)
mysql | MYSQL_SORT_BUFFER_SIZE (default: 256K)
mysql | MYSQL_READ_BUFFER_SIZE (default: 8M or 5% of available memory)
mysql | MYSQL_INNODB_BUFFER_POOL_SIZE (default: 32M or 50% of available memory)
mysql | MYSQL_INNODB_LOG_FILE_SIZE (default: 8M or 15% of available memory)
mysql | MYSQL_INNODB_LOG_BUFFER_SIZE (default: 8M or 15% of available memory)
mysql |
mysql | For more information, see https://github.com/sclorg/mysql-container
我认为这两种方式是等效的,但是第一种方式成功了,第二种方式却失败了,为什么呢?
答案1
您可以为任何环境变量设置默认值在 Compose 文件中引用,或用于配置 Compose在名为的环境文件中
.env
环境.env
不会传递给容器,您只能在中引用它们docker-compose.yml
。
您可以添加env_file: [".env"] to import this file for specific container, but it will be confusing, because that's not what
.env`。您应该:
- 创建一个具有不同名称的文件并在其中引用它
env_file
,或者 添加明确的
environment
条目并引用其中的值.env
:environment: MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"