ERROR: The Compose file '.\docker-compose.yml' is invalid because: Unsupported config option for services.drupal: 'postgres'

ERROR: The Compose file '.\docker-compose.yml' is invalid because: Unsupported config option for services.drupal: 'postgres'

这是我的docker-compose.yml:

version: "3"

services:
    drupal:
        image: drupal
        ports:
        - "8080:80"
        volumes:
        - drupal-modules:/var/www/html/modules
        - drupal-profiles:/var/www/html/profiles
        - drupal-sites:/var/www/html/sites
        - drupal-themes:/var/www/html/themes
        postgres:
            image: postgres
            environment:
                - POSTGRES_PASSWORD=mypasswd

volumes:
    drupal-modules:
    drupal-profiles:
    drupal-sites:
    drupal-themes:

正如我运行

$ docker-compose up

我得到:

ERROR: The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for services.drupal: 'postgres'

有什么问题?

已解决

每次看到

ERROR: The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for

很有可能问题出在缩进错误,就是这种情况。

首先,postgresdrupal都是容器(所以services),所以它们的缩进级别必须相同,即比[=小一个缩进级别14=].
整体少一级缩进postgres structure/tree.

其次,每个以-开头的元素必须比它所属的元素多一级缩进。
所以给 portsvolumes.

的元素再增加一个缩进级别

这里是docker-compose.yml更正:

version: "3"

services:
    drupal:
        image: drupal
        ports:
            - "8080:80"
        volumes:
            - drupal-modules:/var/www/html/modules
            - drupal-profiles:/var/www/html/profiles
            - drupal-sites:/var/www/html/sites
            - drupal-themes:/var/www/html/themes
    postgres:
        image: postgres
        environment:
            - POSTGRES_PASSWORD=mypasswd

volumes:
    drupal-modules:
    drupal-profiles:
    drupal-sites:
    drupal-themes:

docker-compose.yml 中的缩进不正确,因为 .yml 文件对缩进敏感。

你犯了三个错误:

  1. ports是一个key:value数据,所以下一行要缩进-
  2. volumes是一个key:value数据,所以下一行要缩进-
  3. postgres 服务的缩进应与 drupal.
  4. 处于同一级别

所以你的文件应该看起来像这样才能工作:

version: "3"

services:
    drupal:
        image: drupal
        ports:
            - "8080:80"
        volumes:
            - drupal-modules:/var/www/html/modules
            - drupal-profiles:/var/www/html/profiles
            - drupal-sites:/var/www/html/sites
            - drupal-themes:/var/www/html/themes
    postgres:
        image: postgres
        environment:
            - POSTGRES_PASSWORD=mypasswd

volumes:
    drupal-modules:
    drupal-profiles:
    drupal-sites:
    drupal-themes: