docker 运行 --config 与 docker compose config 之间有什么区别?

What is the difference between docker run --config vs docker compose config?

所以我看到的是 docker 运行 命令用于创建一个 docker 容器用于开放遥测传入一个 config 命令,代码看起来像...

$ git clone git@github.com:open-telemetry/opentelemetry-collector.git; \
    cd opentelemetry-collector/examples; \
    go build main.go; ./main & pid1="$!";
    docker run --rm -p 13133:13133 -p 14250:14250 -p 14268:14268 \
      -p 55678-55679:55678-55679 -p 4317:4317 -p 8888:8888 -p 9411:9411 \
      -v "${PWD}/local/otel-config.yaml":/otel-local-config.yaml \
      --name otelcol otel/opentelemetry-collector \
      --config otel-local-config.yaml; \
    kill $pid1; docker stop otelcol

(https://opentelemetry.io/docs/collector/getting-started/#docker)

我不明白的是非docker相关的配置文件(打开遥测配置)如何适合进入“docker 运行 --config”或“docker compose config”命令。下面是打开的遥测配置文件,似乎与docker 无关

extensions:
  memory_ballast:
    size_mib: 512
  zpages:
    endpoint: 0.0.0.0:55679

receivers:
  otlp:
    protocols:
      grpc:
      http:

processors:
  batch:
  memory_limiter:
    # 75% of maximum memory up to 4G
    limit_mib: 1536
    # 25% of limit up to 2G
    spike_limit_mib: 512
    check_interval: 5s

exporters:
  logging:
    logLevel: debug

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [memory_limiter, batch]
      exporters: [logging]
    metrics:
      receivers: [otlp]
      processors: [memory_limiter, batch]
      exporters: [logging]

  extensions: [memory_ballast, zpages]

https://github.com/open-telemetry/opentelemetry-collector/blob/main/examples/local/otel-config.yaml

现在我已经查看了这些 Docker 链接

https://docs.docker.com/engine/swarm/configs/#how-docker-manages-configs

https://nickjanetakis.com/blog/docker-tip-43-using-the-docker-compose-config-command

但我无法弄清楚如何在 open telemetry 开始工作的示例 docker 与 docker 组合配置 组合。这是我的docker撰写

version: "3.9"

services:
  opentelemetry:
    container_name: otel
    image: otel/opentelemetry-collector:latest
    volumes:
      - ~/source/repos/CritterTrackerProject/DockerServices/OpenTelemetry/otel-collector-config.yml:/otel-local-config.yml
    config:
      - otel-local-config.yml
    ports:
      - 13133:13133
      - 14250:14250
      - 14268:14268
      - 55678-55679:55678-55679
      - 4317:4317
      - 8888:8888
      - 9411:9411
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - my-network

  jaeger:
    # restart: unless-stopped
    container_name: jaeger
    image: jaegertracing/all-in-one:latest
    ports:
      - 16686:16686
      # - 14250:14250
      # - 14268:14268
      # - 5775:5775/udp
      - 6831:6831/udp
      # - 6832:6832/udp
      # - 5778:5778
      # - 9411:9411
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - my-network

  postgres:
   restart: always
   container_name: postgres
   image: postgres:latest
   environment:
     - POSTGRES_USER=code
     - POSTGRES_PASSWORD=code
   ports:
     - 5432:5432
   volumes: 
     - postgres:/var/lib/postgresql/data
   extra_hosts:
     - "host.docker.internal:host-gateway"
   networks:
     - my-network

  nginx:
    restart: always
    container_name: webserver
    image: nginx:latest
    build:
      context: ~/source/repos/CritterTrackerProject
      dockerfile: DockerServices/Nginx/Dockerfile
    ports:
      - 80:80
      - 443:443
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - my-network

volumes:
  postgres:

networks:
  my-network:
    external: true
    name: my-network

这是我在 运行ning docker compos up 之后的错误 Git Bash terminal

$ docker compose -f ./DockerServices/docker-compose.yml up -d
services.opentelemetry Additional property config is not allowed

您在 docker 运行 命令中所做的是以下挂载:

${PWD}/local/otel-config.yaml 从本地主机到 /otel-local-config.yaml 从 docker

您可以使用 docker compose 中的 volumes 选项实现相同的行为:

数量:

  • "${PWD}/local/otel-config.yaml":/otel-local-config.yaml

docker run的一般形式是

docker run [docker options] image [command]

如果您查看原始命令,它会匹配此模式

docker run \
  --rm -p ... -v ... --name ... \  # Docker options
  otel/opentelemetry-collector \   # Image
  --config otel-local-config.yaml  # Command

所以看起来像 --config 选项的实际上是容器设置的命令部分;它覆盖了 Dockerfile CMD,它是 passed as additional arguments to the image's ENTRYPOINT.

然后,在 Compose 设置中,这将是容器的 command:

services:
  opentelemetry:
    image: otel/opentelemetry-collector:latest
    command: --config otel-local-config.yaml

因为这是一个 application-specific 命令字符串,它与 docker-compose config 命令无关,后者是一个诊断工具,只会转储部分 Compose 配置。