Docker运行正常但docker-compose启动MySQL容器失败求助
Ah, I spot the problem right away! Your docker-compose.yml has a small but critical syntax mistake in the environment section that's making MySQL think you didn't provide a root password.
The Issue
When using the array format (with - prefix) for environment variables in docker-compose, you need to use the KEY=VALUE syntax instead of a colon. Your line - MYSQL_ROOT_PASSWORD:12345678 uses a colon, which isn't recognized correctly in this context—Docker Compose isn't passing the password to the MySQL container properly, hence the "database is uninitialized and password option is not specified" error.
Correct Docker-Compose Config
You can fix this in two ways:
Option 1: Array Format with =
version: "3" services: mysql: image: mysql:5.7.21 environment: - MYSQL_ROOT_PASSWORD=12345678 container_name: some-mysql
Option 2: Key-Value Format (No - prefix)
If you prefer the YAML key-value style, you can omit the hyphens and write it like this:
version: "3" services: mysql: image: mysql:5.7.21 environment: MYSQL_ROOT_PASSWORD: 12345678 container_name: some-mysql
How to Apply the Fix
- Update your
docker-compose.ymlwith either of the correct formats above. - First, clean up any existing containers from your previous attempt (to avoid conflicts):
docker-compose down - Start the container again:
docker-compose up -d
This should resolve the initialization error and get your MySQL container running just like it did with the docker run command.
内容的提问来源于stack exchange,提问作者AngeloC

