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

Docker Compose 3+版本docker-compose.yml空格语法规范咨询

Docker Compose 3+ 配置文件空格语法规范

Great question! Let's break this down clearly since conflicting online examples can make YAML's spacing rules feel confusing.

First off, remember that docker-compose.yml is a YAML file at its core—so all its syntax rules follow the YAML specification. Here's what you need to know about spacing:

  • No tabs allowed: You already got this right—YAML explicitly prohibits tabs for indentation; only spaces work. Using tabs will cause parsing errors every time.
  • No strict mandatory space count per level: The YAML spec doesn't force 2 spaces per indentation level. You could technically use 4 spaces, or even 3 (though that's pretty unusual), as long as you're consistent across the entire file.
  • 2 spaces per level is the de facto standard: While not strictly required, Docker's official documentation and almost all community examples use 2 spaces for indentation. This is a widely adopted convention because:
    • It's compact and keeps the file readable even with deeply nested structures
    • It aligns with common style guides for other languages/tools (like Python, React JSX, etc.)
    • It ensures consistency when collaborating with other developers who expect this standard
  • Critical spacing rule: Colons need a space: Always add a single space after a colon when defining key-value pairs. For example, image: nginx is valid, but image:nginx will throw a parsing error immediately.

Here's a quick example of a properly formatted Compose file (using 2-space indents):

version: "3.8"
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html

Just to reiterate: As long as you stick to spaces (no tabs) and keep your indentation consistent throughout the file, YAML will parse it correctly. But using 2 spaces per level is strongly recommended to align with community best practices and avoid unnecessary confusion.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:34:27