Docker卷必须为映射而非字符串错误排查(附docker-compose配置)
Let's break down what's causing this error and fix your docker-compose.yaml step by step:
1. The Root Cause of the Volume Error
The error message points directly to the last line of your file: volumes:... — this is invalid YAML syntax for defining volumes. When using named volumes (like your db_data), you need to explicitly declare them in the top-level volumes section, even if you don't add any extra configuration for them.
2. Critical Typos Breaking File Parsing
You've got two typos that are throwing off Docker Compose's ability to read the file correctly (which can also contribute to confusing error messages):
serivces→ should beservices(missing the letter 'c')evironment(under thedbservice) → should beenvironment(missing the letter 'n')
Corrected docker-compose.yaml File
Here's the fully fixed version of your Compose file:
version: '3.3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" volumes: - ./:/var/www/html restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
Key Fixes Explained:
- Valid Volume Declaration: Replaced
volumes:...withvolumes: db_data:— this tells Docker to create and manage the persistentdb_datavolume for MySQL. - Fixed Service Key:
servicesis the correct top-level key for defining your containers, so Docker can now properly read yourdbandwordpressservice configurations. - Fixed Environment Key: Correcting
evironmenttoenvironmentensures the database's password and connection settings are applied properly.
After updating your file, run docker-compose up -d again — this should resolve the volume error and start your WordPress and MySQL containers successfully.
内容的提问来源于stack exchange,提问作者user818700

