部署到Service Fabric时,如何在docker-compose文件中设置有效ServiceDnsName?
I've run into this exact issue before when deploying Docker Compose apps to Service Fabric via Azure DevOps (formerly VSTS)—let's walk through solving it and clarify how to set valid ServiceDnsName values.
First, Why You're Seeing This Error
Service Fabric enforces strict RFC 1123-compliant rules for DNS names, even when deploying via Compose. The key constraints are:
- Only lowercase letters, numbers, and hyphens (
-) are allowed - Names can't start or end with a hyphen
- If using a fully qualified domain name (FQDN) with dots (
.), each segment must follow these rules
Your service name myapp seems valid at first glance, but the error usually happens because either:
- Service Fabric's automatic mapping from Compose service name to
ServiceDnsNameis breaking (e.g., hidden uppercase characters, or older task versions) - You haven't explicitly defined the DNS name, and the default mapping is failing
How to Explicitly Set a Valid ServiceDnsName in docker-compose.yml
To avoid automatic mapping issues, define the DNS name directly using Service Fabric's Compose extensions. Here's how:
Add a deploy.x-ms-service-fabric section under your service, and specify serviceDnsName with a compliant value:
Example Valid Compose Snippet
services: myapp: image: your-container-registry/myapp:${BUILD_BUILDNUMBER} ports: - "8080:80" deploy: replicas: 2 x-ms-service-fabric: # Option 1: Simple valid DNS name serviceDnsName: "myapp" # Option 2: Fully qualified domain name (each segment must be compliant) # serviceDnsName: "myapp.my-services.sf-cluster.local"
Valid vs. Invalid ServiceDnsName Examples
- ✅ Valid:
myapp,my-app,myapp.api,my-app.prod.sf-cluster.local - ❌ Invalid:
MyApp(uppercase),my_app(underscores),my-app.(ends with dot),.myapp(starts with dot),my app(spaces)
Troubleshooting Your Specific Error with 'myapp'
Since myapp is technically compliant, try these fixes:
- Explicitly set the
serviceDnsNameas shown above—this overrides any flawed automatic mapping in the deployment task. - Check for hidden characters in your Compose file's service name (e.g., accidental uppercase letters, trailing spaces).
- Update the Service Fabric Compose Deploy task in your Azure DevOps release pipeline—older versions had bugs with name mapping. Use the latest version (v2 or newer).
- Verify cluster DNS is enabled—Service Fabric clusters have DNS enabled by default, but if you're using a custom cluster, double-check this setting in the Azure portal.
Key Note About FQDNs in Compose
As your reference guide mentioned, if you use an FQDN with dots in your Compose service name, Service Fabric will register that as the DNS name—but only if each segment follows the RFC 1123 rules. Explicitly defining serviceDnsName is always safer to avoid surprises.
内容的提问来源于stack exchange,提问作者Andrew Roth

