| 1 | #!/bin/bash
|
| 2 | DOCKER_BASE="${DOCKER_BASE:-/srv/docker}"
|
| 3 | TRAEFIK_DIR="${TRAEFIK_DIR:-traefik}"
|
| 4 |
|
| 5 | mkdir -p ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf
|
| 6 |
|
| 7 | touch ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf/.env
|
| 8 | ln -s container.conf/.env ${DOCKER_BASE}/${TRAEFIK_DIR}/
|
| 9 |
|
| 10 | cat > ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf/docker-compose.yml <<EOF
|
| 11 | # Inspired by https://containo.us/blog/traefik-2-0-docker-101-fc2893944b9d/
|
| 12 |
|
| 13 | version: '3.7'
|
| 14 |
|
| 15 | services:
|
| 16 | traefik:
|
| 17 | image: traefik:2.6
|
| 18 | command:
|
| 19 | - --entrypoints.web.address=:80
|
| 20 | - --entrypoints.websecure.address=:443
|
| 21 | - "--log.level=INFO"
|
| 22 | - --providers.docker=true
|
| 23 | - --providers.docker.exposedbydefault=false
|
| 24 | - --api=true
|
| 25 | - --certificatesresolvers.default.acme.caserver=\${LEAPI:-https://acme-v02.api.letsencrypt.org/directory}
|
| 26 | - --certificatesresolvers.default.acme.email=\${LEMAIL:-ssladmin@test.org}
|
| 27 | - --certificatesresolvers.default.acme.storage=/etc/traefik/acme.json
|
| 28 | - --certificatesresolvers.default.acme.tlschallenge=true
|
| 29 | - --providers.file.filename=/etc/traefik/traefik_providers.yaml
|
| 30 | - --providers.file.watch=true
|
| 31 | logging:
|
| 32 | options:
|
| 33 | max-size: "100M"
|
| 34 | max-file: "10"
|
| 35 | networks:
|
| 36 | - system_traefik
|
| 37 | environment:
|
| 38 | - LC_ALL=C.UTF-8
|
| 39 | - TZ=Europe/Berlin
|
| 40 | labels:
|
| 41 | # Enable Traefik for it's own backend
|
| 42 | - traefik.enable=true
|
| 43 | # Dashboard
|
| 44 | - traefik.http.routers.traefik.rule=Host(\`traefik.test.org\`)
|
| 45 | - traefik.http.routers.traefik.entrypoints=websecure
|
| 46 | - traefik.http.routers.traefik.tls=true
|
| 47 | - traefik.http.routers.traefik.tls.certresolver=default
|
| 48 | - traefik.http.routers.traefik.service=api@internal
|
| 49 | # Basic auth for dashboard
|
| 50 | - traefik.http.routers.traefik.middlewares=authtraefik@docker,default-security-headers@file
|
| 51 | # middleware authtraefik
|
| 52 | - traefik.http.middlewares.authtraefik.basicauth.users=\${DASHBOARD_USERS:-admin:\$\$apr1\$\$AAbCdQpX\$\$ajelS9mMisKRG.lqcY/uXU/} # user/password
|
| 53 | ports:
|
| 54 | - "80:80"
|
| 55 | - "443:443"
|
| 56 | restart: always
|
| 57 | volumes:
|
| 58 | - "./config/:/etc/traefik/"
|
| 59 | - "/var/run/docker.sock:/var/run/docker.sock:ro"
|
| 60 |
|
| 61 | networks:
|
| 62 | system_traefik:
|
| 63 | external: true
|
| 64 | EOF
|
| 65 | ln -s container.conf/docker-compose.yml ${DOCKER_BASE}/${TRAEFIK_DIR}/
|
| 66 |
|
| 67 | cat > ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf/production.yml <<EOF
|
| 68 | version: '3.7'
|
| 69 |
|
| 70 | services:
|
| 71 |
|
| 72 | traefik:
|
| 73 | labels:
|
| 74 | # Allow watchtower to update this image
|
| 75 | - com.centurylinklabs.watchtower.enable=true
|
| 76 | # See https://docs.traefik.io/migration/v1-to-v2/#strip-and-rewrite-path-prefixes
|
| 77 | - traefik.http.routers.traefik.rule=Host(\`$(hostname -f)\`) && (PathPrefix(\`/traefik\`) || PathPrefix(\`/api\`))
|
| 78 | # Redefine middleware for router 'traefik' as we add more middlewares
|
| 79 | - traefik.http.routers.traefik.middlewares=authtraefik@docker,traefik-dashboard-stripprefix@file,default-security-headers@file
|
| 80 | EOF
|
| 81 |
|
| 82 | cat > ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf/traefik.service <<EOF
|
| 83 | [Unit]
|
| 84 | Description=Traefik Proxy Service
|
| 85 | After=network.target docker.service
|
| 86 | Requires=docker.service
|
| 87 |
|
| 88 | [Service]
|
| 89 | Type=oneshot
|
| 90 | RemainAfterExit=yes
|
| 91 |
|
| 92 | Environment="WORK_DIR=/srv/docker/traefik/"
|
| 93 | WorkingDirectory=/srv/docker/traefik/
|
| 94 | ExecStartPre=/bin/bash -c "/usr/bin/docker network inspect system_traefik &>/dev/null || /usr/bin/docker network create --driver bridge system_traefik"
|
| 95 | ExecStartPre=-/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" down
|
| 96 | ExecStart=/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" up -d
|
| 97 | ExecStop=/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" down
|
| 98 |
|
| 99 | [Install]
|
| 100 | WantedBy=docker.service
|
| 101 | EOF
|
| 102 | ln -s ${DOCKER_BASE}/${TRAEFIK_DIR}/container.conf/traefik.service /etc/systemd/system/
|
| 103 |
|
| 104 | mkdir -p ${DOCKER_BASE}/${TRAEFIK_DIR}/config
|
| 105 |
|
| 106 | cat > ${DOCKER_BASE}/${TRAEFIK_DIR}/config/traefik_providers.yaml <<EOF
|
| 107 | ---
|
| 108 | tls:
|
| 109 | options:
|
| 110 | default:
|
| 111 | minVersion: VersionTLS12
|
| 112 | sniStrict: true
|
| 113 | cipherSuites:
|
| 114 | # TLS 1.2 cipher suites.
|
| 115 | - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
|
| 116 | - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
|
| 117 | - TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305
|
| 118 | # IE 11 and Safari < 9 + iOS <9, OSX < 10.11
|
| 119 | - TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
|
| 120 | # TLS 1.3 cipher suites.
|
| 121 | - TLS_AES_128_GCM_SHA256
|
| 122 | - TLS_AES_256_GCM_SHA384
|
| 123 | - TLS_CHACHA20_POLY1305_SHA256
|
| 124 | # TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator that the client is doing version fallback. See RFC 7507.
|
| 125 | - TLS_FALLBACK_SCSV
|
| 126 | curvePreferences:
|
| 127 | - CurveP521
|
| 128 | - CurveP384
|
| 129 |
|
| 130 | http:
|
| 131 | middlewares:
|
| 132 | redirect-web-to-websecure:
|
| 133 | redirectScheme:
|
| 134 | scheme: https
|
| 135 | permanent: true
|
| 136 | default-security-headers:
|
| 137 | headers:
|
| 138 | accessControlAllowMethods:
|
| 139 | - GET
|
| 140 | - POST
|
| 141 | - DELETE
|
| 142 | - OPTIONS
|
| 143 | accessControlAllowOriginList: ["<origin>"]
|
| 144 | accessControlMaxAge: 100
|
| 145 | browserXssFilter: true
|
| 146 | contentTypeNosniff: true
|
| 147 | forceSTSHeader: true
|
| 148 | # frameDeny: true
|
| 149 | # sslRedirect: true
|
| 150 | stsIncludeSubdomains: true
|
| 151 | stsPreload: true
|
| 152 | stsSeconds: 315360000
|
| 153 | # contentSecurityPolicy: "default-src 'self' 'unsafe-inline';script-src 'self' 'unsafe-inline' 'unsafe-eval';img-src 'self' data:;font-src 'self' data:;connect-src 'self' ws: wss:"
|
| 154 | # customRequestHeaders:
|
| 155 | # X-Frame-Options: "SAMEORIGIN"
|
| 156 | # customFrameOptionsValue: "SAMEORIGIN"
|
| 157 | referrerPolicy: "same-origin"
|
| 158 | permissionsPolicy: "vibrate=(self)"
|
| 159 | traefik-dashboard-stripprefix:
|
| 160 | stripPrefix:
|
| 161 | prefixes:
|
| 162 | - "/traefik"
|
| 163 | services:
|
| 164 | redirect-dummy:
|
| 165 | loadBalancer:
|
| 166 | servers:
|
| 167 | - url: ""
|
| 168 | routers:
|
| 169 | # global redirect to https
|
| 170 | # per domain see https://doc.traefik.io/traefik/migration/v1-to-v2/#http-to-https-redirection-is-now-configured-on-routers
|
| 171 | web-to-websecure:
|
| 172 | rule: "hostregexp(\`{host:.+}\`)"
|
| 173 | service: "redirect-dummy@file"
|
| 174 | entryPoints:
|
| 175 | - "web"
|
| 176 | middlewares:
|
| 177 | - redirect-web-to-websecure@file
|
| 178 | EOF
|
| 179 |
|
| 180 | systemctl daemon-reload && systemctl enable traefik && systemctl start traefik
|
| 181 |
|