【问题标题】:I have a docker-compose file when i am running i am seeing below error can some one help on it我在运行时有一个 docker-compose 文件,我看到下面的错误,有人可以帮忙吗
【发布时间】:2019-09-30 15:15:35
【问题描述】:

当我尝试构建时看到以下错误。也尝试过更改多个版本,但没有用。

docker-compose up --build

错误:撰写文件“./docker-compose.yml”无效,因为: 无效的顶级属性“worker”。此 Compose 文件的有效顶级部分是:版本、服务、网络、卷、机密、配置和以“x-”开头的扩展。

您可能会看到此错误,因为您使用了错误的 Compose 文件版本。指定支持的版本(例如“2.2”或“3.3”)并将您的服务定义放在services 键下,或者省略version 键并将您的服务定义放在文件的根目录以使用版本1。 有关 Compose 文件格式版本的更多信息,请参阅https://docs.docker.com/compose/compose-file/

以下是我的撰写文件

version: '3'
services:
  postgres: 
    image: 'postgres:latest'
  redis:
    image: 'redis:latest'
  nginx:
    restart: always  
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - /app/node_modules
      - ./server:/app
    environment: 
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - PGUSER=postgres
      - PGHOST=postgres
      - PGDATABASE=postgres
      - PGPASSWORD=postgres_password
      - PGPORT=5432
client:
  build:
    dockerfile: Dockerfile.dev
    context: ./client
  volumes:
    -  /app/node_modules
    - ./client:/app
worker:
  build:
    dockerfile: Dockerfile.dev
    context: ./worker
  volumes: 
    - /app/node_modules
    - ./worker:/app

【问题讨论】:

  • YAML 文件无效。请检查client: 及以下的缩进(client: 及以下的每行多留两个空格)。
  • 添加了,一旦我用 yaml lint 检查了它是完美的,你能检查一下吗
  • 但是对于 docker 来说并不完美。见我上面的评论。 client:worker: 必须在服务下(不在同一级别)。

标签: docker-compose


【解决方案1】:

您的 docker-compose 文件存在缩进问题。 “工人”和“客户”是服务,因此必须作为儿童的“服务”。以下应该可以工作。

version: '3'
services:
  postgres: 
    image: 'postgres:latest'
  redis:
    image: 'redis:latest'
  nginx:
    restart: always  
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - /app/node_modules
      - ./server:/app
    environment: 
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - PGUSER=postgres
      - PGHOST=postgres
      - PGDATABASE=postgres
      - PGPASSWORD=postgres_password
      - PGPORT=5432
  client:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      -  /app/node_modules
      - ./client:/app
  worker:
    build:
      dockerfile: Dockerfile.dev
      context: ./worker
    volumes: 
      - /app/node_modules
      - ./worker:/app

【讨论】:

  • 谢谢,虽然我已经交叉验证了缩进,但工作正常,谢谢
  • 什么错误?此外,如果该解决方案适合您,您可能希望接受该解决方案作为答案。
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 2022-12-13
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多