You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

docker-compose.yml中configs的content属性不被允许的报错问题

Docker Compose配置报错分析与解决

问题描述

用户编写的docker-compose.yml配置:

version: "3.8"
services:
  test:
    image: busybox:latest
    configs:
      - source: cfg
        target: /opt/cfg.json
        mode: "0644"
configs:
  cfg:
    content: "{}"

执行命令docker compose -f docker-compose.yml up时出现报错:

validating /opt/docker-compose.yml: configs.cfg Additional property content is not allowed

用户环境版本:

Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
Docker Compose version v2.21.0

报错原因

configs顶层定义中的content属性是Docker Swarm模式专属配置项,仅在Swarm集群环境下生效。用户当前处于单节点非Swarm模式,Compose v2不支持在此模式下使用content直接定义配置内容,因此触发校验报错。

解决方法

提供两种适配不同场景的解决方案:

方案1:切换到Swarm模式(适合集群部署场景)

若需要使用Swarm的配置管理能力,先初始化Swarm环境,再部署服务:

# 初始化Swarm(单节点也可启用)
docker swarm init
# 部署服务
docker compose -f docker-compose.yml up

Swarm模式下,content属性可正常使用,配置内容会被Swarm集群管理并分发到服务容器中。

方案2:使用本地文件替代content(适合单节点非Swarm场景)

无需切换Swarm模式,将content替换为file属性,指向本地配置文件:

  1. 本地创建cfg.json文件,内容为{}
  2. 修改docker-compose.yml的configs部分:
version: "3.8"
services:
  test:
    image: busybox:latest
    configs:
      - source: cfg
        target: /opt/cfg.json
        mode: "0644"
configs:
  cfg:
    file: ./cfg.json

Compose会读取本地文件内容作为配置,挂载到容器指定路径,无需Swarm模式即可正常运行。

或者直接改用volume挂载本地文件,更适合简单场景:

version: "3.8"
services:
  test:
    image: busybox:latest
    volumes:
      - ./cfg.json:/opt/cfg.json:ro

内容的提问来源于stack exchange,提问作者Selivanov Pavel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 10:25:19