Traefik自签名证书

时间:2020-06-05 15:58:34

标签: docker ssl traefik

我正在尝试让我的Docker容器之一使用自定义自签名SSL。我已经按照从网上浏览收集到的一些说明进行了操作,并且其他所有操作都正常,但是我的容器一直使用Traefik默认证书,而不是我想要使用的自定义证书。我该如何实现?

老实说,我不清楚Docker容器如何选择和使用SSL证书,而且我阅读的文档似乎也不清楚。这是我到目前为止所做的。

用于Traefik的Docker Compose yml文件

version: '3'
services:
  traefik:
    image: "traefik:v2.2"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ~/docker-data/traefik/traefik.yml:/etc/traefik/traefik.yaml:ro
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`traefik.com`)"
      - "traefik.http.routers.api.entrypoints=insecure"
      - "traefik.http.routers.api.service=api@internal"
      - "traefik.http.routers.api.middlewares=api-auth"
      - "traefik.http.middlewares.api-auth.basicauth.users=admin:..."
    container_name: traefik
networks:
  default:
    external:
      name: "web"

我的Traefik yml文件

providers: # You can add more than one provider if needed
  docker:
    endpoint: "unix:///var/run/docker.sock"
    network: "web" # Custom docker network
    exposedByDefault: false # Only expose explicitly enabled containers

  file:
    filename: ~/docker-data/traefik/dynamic_conf.yml
    watch: true

entryPoints:
  insecure:
    address: ":80"
  secure:
    address: ":443"

api:
  dashboard: true

我的动态配置文件

# Dynamic configuration
tls:
  certificates:
    - certFile: "~/ssl/bwtest-cert.pem"
      keyFile: "~/ssl/bwtest-key.pem"

我的Docker容器

---
version: '3'
services:
  bwtest:
    image: bitwardenrs/server
    restart: always
    container_name: bwtest
    volumes:
      - bw-test-data:/data
#      - bw-test-ssl:/ssl
    environment:
      LOG_FILE: '/data/bw.log'
      SHOW_PASSWORD_HINT: 'true'
    labels:

      - traefik.enable=true
      - traefik.docker.network=web
      - traefik.http.middlewares.redirect-https.redirectScheme.scheme=https
      - traefik.http.middlewares.redirect-https.redirectScheme.permanent=true
      - traefik.http.routers.bitwarden-ui-https.rule=Host(`bwtest.com`)
      - traefik.http.routers.bitwarden-ui-https.entrypoints=secure
      - traefik.http.routers.bitwarden-ui-https.tls=true
      - traefik.http.routers.bitwarden-ui-https.service=bwtest
      - traefik.http.routers.bitwarden-ui-http.rule=Host(`bwtest.com`)
      - traefik.http.routers.bitwarden-ui-http.entrypoints=insecure
      - traefik.http.routers.bitwarden-ui-http.middlewares=redirect-https
      - traefik.http.routers.bitwarden-ui-http.service=bwtest
      - traefik.http.services.bwtest.loadbalancer.server.port=80
      - traefik.http.routers.mydomain.tls.domains[0].main=bwtest.com
      - traefik.http.routers.mydomain.tls.domains[0].sans=www.bwtest.com
    networks:
      - web
networks:
  web:
    external: true
volumes:
  bw-test-data:
  bw-test-ssl:

1 个答案:

答案 0 :(得分:0)

我建议尝试将证书移至默认存储(https://docs.traefik.io/https/tls/#default-certificate):

tls:
  stores:
    default:
      defaultCertificate:
        certFile: path/to/cert.crt
        keyFile: path/to/cert.key