首页 > 解决方案 > Compose 文件 '.\docker-compose.yml' 无效,因为:顶级属性“db”无效

问题描述

我的 docker-compose.yml 文件包含

    version: '3.8'
    services:
      web:
        build: .
        command: python /code/manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - 8000:8000
        depends_on:
          - db
    db:
      image: postgres
      volumes:
        - postgres_data:/var/lib/postgresql/data/
    volumes:
      postgres_data:

Dockerfile 包含

     # Pull base image
        FROM python:3.8.6
        # Set environment variables
        ENV PYTHONDONTWRITEBYTECODE 1
        ENV PYTHONUNBUFFERED 1
        # Set work directory
        WORKDIR /code
        # Install dependencies
        COPY Pipfile Pipfile.lock /code/
        RUN pip install pipenv && pipenv install --system
        # Copy project
        COPY . /code/
    
Using that command to build image and run the containers with one command

    $ docker-compose up -d --build
    

和 gitbash 终端抛出该错误

     $ docker-compose up -d --build
        The Compose file '.\docker-compose.yml' is invalid because:
        Invalid top-level property "db". Valid top-level sections for this Compose file are: version, services, networks, volumes, secrets, configs, and extensions starting with "x-".
        
        You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key or omit the `version` key and place your service definitions at the root of the file to use version 1.
        

我的 docker-compose.yml 文件有问题。主要是那个部分” 指定支持的版本(例如“2.2”或“3.3”)并将您的服务定义放在services键下,或者省略version键并将您的服务定义放在文件的根目录以使用版本 1。关于 Compose 文件格式版本,请参阅https://docs.docker.com/compose/compose-file/"。请帮助进行更改以使其正常工作。谢谢

标签: pythondjangodockerdocker-compose

解决方案


推荐阅读