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

如何配置Traefik为本地Docker容器生成多域名主机规则?

Multiple Hostnames for Docker Containers with Traefik & Docker Compose

Absolutely! You can totally configure Traefik to generate multiple host rules for your Docker containers—this is exactly what you need for accessing your CouchDB instance from both your local machine and the Android emulator. There are two straightforward ways to set this up:

Option 1: Separate Routers for Each Hostname

This approach creates distinct Traefik routers for each hostname, which is handy if you ever want to add unique middleware (like authentication) to one of the routes later.

Add these labels to your CouchDB service in docker-compose.yml:

services:
  couchdb:
    image: couchdb:latest
    labels:
      # Enable Traefik for this service
      - "traefik.enable=true"
      
      # Router for local machine access (127.0.0.1.nip.io)
      - "traefik.http.routers.couchdb-local.rule=Host(`couchdb.myproject.127.0.0.1.nip.io`)"
      - "traefik.http.routers.couchdb-local.service=couchdb"
      
      # Router for Android emulator access (10.0.2.2.nip.io)
      - "traefik.http.routers.couchdb-emulator.rule=Host(`couchdb.myproject.10.0.2.2.nip.io`)"
      - "traefik.http.routers.couchdb-emulator.service=couchdb"
      
      # Define the backend service (points to CouchDB's default port)
      - "traefik.http.services.couchdb.loadbalancer.server.port=5984"

Option 2: Single Router with Combined Host Rule

If you don’t need separate configurations for each hostname, you can combine both rules into a single router using Traefik’s || (OR) operator. This keeps your labels more concise:

services:
  couchdb:
    image: couchdb:latest
    labels:
      - "traefik.enable=true"
      # Combine both hostnames in one rule
      - "traefik.http.routers.couchdb.rule=Host(`couchdb.myproject.127.0.0.1.nip.io`) || Host(`couchdb.myproject.10.0.2.2.nip.io`)"
      - "traefik.http.services.couchdb.loadbalancer.server.port=5984"

Verify Your Traefik Setup

Make sure your Traefik service is configured to listen to Docker events (the Docker provider is enabled). Here’s a minimal Traefik service configuration for reference:

traefik:
  image: traefik:v2.11
  command:
    - "--api.insecure=true" # Optional: for accessing the Traefik dashboard at http://localhost:8080
    - "--providers.docker=true"
    - "--providers.docker.exposedbydefault=false" # Only expose services with traefik.enable=true
    - "--entrypoints.web.address=:80" # Listen on port 80 for HTTP traffic
  ports:
    - "80:80"
    - "8080:8080"
  volumes:
    - "/var/run/docker.sock:/var/run/docker.sock:ro" # Give Traefik access to Docker socket

Once you’ve updated your docker-compose.yml, run docker-compose up -d to apply the changes. Now you can access your CouchDB instance via both:

  • http://couchdb.myproject.127.0.0.1.nip.io (from your local machine)
  • http://couchdb.myproject.10.0.2.2.nip.io (from the Android emulator, since 10.0.2.2 maps to your host machine’s loopback interface)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:38:35