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

如何在多组合Docker Compose文件中覆盖端口配置取消暴露?

How to Reuse Staging Build Config but Disable Port Exposure in Docker Compose Production Setup

As you noted in the Docker Compose documentation:

For the multi-value options ports, expose, external_links, dns, dns_search, and tmpfs, Compose concatenates both sets of values

This is exactly why setting ports: [] in your docker-compose.prod.yml didn't work—it just appended an empty list to the existing 3306:3306 mapping from docker-compose.staging.yml, rather than removing it. Here are two clean ways to solve this without duplicating configuration:

Option 1: Use null to Reset the ports Property (Simplest Fix)

Starting with Docker Compose v2+, you can set a multi-value property to null in an override file to completely clear all previous values from earlier compose files. Update your docker-compose.prod.yml to this:

version: "3.4"
services:
  mysql:
    ports: null

Now when you run your production command:

docker-compose -f docker-compose.yml -f docker-compose.staging.yml -f docker-compose.prod.yml up -d

The ports: null entry will erase the port mapping from docker-compose.staging.yml, while preserving the build: target: prod configuration you want to reuse from staging.

Option 2: Refactor Configs for Explicit Reuse

If you prefer a more explicit setup that avoids "undoing" settings, split your staging configuration into two separate files to separate shared prod build settings from staging-specific ports:

  1. Create docker-compose.prod-build.yml (shared between staging and production):

    version: "3.4"
    services:
      mysql:
        build:
          target: prod
    
  2. Update docker-compose.staging.yml to only contain staging-specific settings:

    version: "3.4"
    services:
      mysql:
        ports:
          - 3306:3306
    

Now you can launch each environment without overlapping settings:

  • Staging:
    docker-compose -f docker-compose.yml -f docker-compose.prod-build.yml -f docker-compose.staging.yml up -d
    
  • Production:
    docker-compose -f docker-compose.yml -f docker-compose.prod-build.yml up -d
    

This approach makes your configuration easier to maintain, as each file handles a clear, distinct part of your setup.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:42:37