簡體   English   中英

502代理錯誤(docker + traefik + apache)

[英]502 Proxy Error ( docker + traefik + apache )

我正在嘗試為本地開發實例上的SSL終止設置traefik。 遵循指南,我有以下配置。

泊塢窗,compose.yml

version: '2.1'
services:
  mariadb:
    image: wodby/mariadb:10.2-3.0.2
    healthcheck:
        test: "/usr/bin/mysql --user=dummyuser --password=dummypasswd --execute \"SHOW DATABASES;\" | grep database"
        interval: 3s
        timeout: 1s
        retries: 5
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: dummy
      MYSQL_DATABASE: database
    volumes:
      - ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
      - mysql:/var/lib/mysql # I want to manage volumes manually.

  php:
    depends_on:
      mariadb:
        condition: service_healthy
    ports:
        - "25:25"
        - "587:587"
    environment:
      PHP_FPM_CLEAR_ENV: "no"
      DB_HOST: mariadb
      #DB_USER: dummy
      DB_PASSWORD: dummypasswd
      DB_NAME: database
      DB_DRIVER: mysql
      PHP_POST_MAX_SIZE: "256M"
      PHP_UPLOAD_MAX_FILESIZE: "256M"
      PHP_MAX_EXECUTION_TIME: 300
    volumes:
      - codebase:/var/www/html/
      - private:/var/www/html/private
  solr:
    image: mxr576/apachesolr-4.x-drupal-docker
    ports:
      - "8983:8983"
    labels:
      - 'traefik.backend=solr'
      - 'traefik.port=8983'
     # - 'traefik.frontend.rule=Host:192.168.33.10'
    volumes:
      - solr:/opt/solr/example/solr/collection1/data
    restart: always
  portainer:
    image: portainer/portainer
    command: --no-auth -H unix:///var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - 'traefik.backend=portainer'
      - 'traefik.port=9000'
    restart: always
      apache:
        image: wodby/php-apache:2.4-2.0.2
    #    ports:
    #      - "80:80"
        depends_on:
          - php
        environment:
          APACHE_LOG_LEVEL: warn
          APACHE_BACKEND_HOST: php
          APACHE_SERVER_ROOT: /var/www/html/drupal
        volumes:
          - codebase:/var/www/html/
          - private:/var/www/html/private
        labels:
          - 'traefik.backend=apache'
          - 'traefik.docker.network=proxy'
          - "traefik.frontend.rule=Host:127.0.0.1"
          - "traefik.enable=true"
          - "traefik.port=80"
          - "traefik.default.protocol=http"
        restart: always
        networks:
          - proxy
      traefik:
        image: traefik
        command: -c /traefik.toml --web --docker --logLevel=INFO
        ports:
          - '80:80'
          - '443:443'
          - '8888:8080' # Dashboard
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - /codebase/traefik.toml:/traefik.toml
          - /codebase/certs/cert.crt:/cert.crt
          - /codebase/certs/cert.key:/cert.key
    volumes:
      solr:
        external: true
      mysql:
        external: true
      codebase:
        external: true
      private:
        external: true

    networks:
      proxy:
        external: true

traefik.toml

logLevel = "DEBUG" # <---
defaultEntryPoints = ["https", "http"] # <---

[accessLog]
[traefikLog]

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
  [entryPoints.https.tls]
    [[entryPoints.https.tls.certificates]]
      certFile = "/cert.crt"
      keyFile = "/cert.key"

[retry]

[docker]
endpoint = "unix:///var/run/docker.sock"
watch = true
exposedbydefault = false

嘗試驗證實例時,我收到502錯誤網關

 curl -i -k https://127.0.0.1
        HTTP/1.1 502 Bad Gateway
        Content-Length: 392
        Content-Type: text/html; charset=iso-8859-1
        Date: Fri, 14 Sep 2018 16:34:36 GMT
        Server: Apache/2.4.29 (Unix) LibreSSL/2.5.5
        X-Content-Type-Options: nosniff
        <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
        <html><head>
        <title>502 Proxy Error</title>
        </head><body>
        <h1>Proxy Error</h1>
        <p>The proxy server received an invalid
        response from an upstream server.<br />
        The proxy server could not handle the request <em><a href="/index.php">GET&nbsp;/index.php</a></em>.<p>
        Reason: <strong>DNS lookup failure for: php</strong></p></p>
        </body></html>

重置docker-compose和docker網絡無濟於事。 我已經在他們的倉庫中檢查了這個問題 ,似乎沒人能找到確定的解決方案。 有人對如何解決這個問題有想法嗎?

編輯:更新完整的docker-compose文件。

您正在嘗試使用服務發現從apache服務連接到php容器。 但是php容器未附加到網絡proxy ,因為尚未為其聲明網絡。 mariabd也是如此。 因此,當您連接到apache/traefik它們會查找未附加到網絡proxy主機php並拋出錯誤502

除非且直到您指定外部網絡,否則Docker容器將不會連接到它們。

因此,您必須為所有服務指定以下網絡,以使docker服務發現正常工作。

networks:
      - proxy

獎金:

既然您已經完成了端口映射。 您還可以使用主機的公共Ip,然后使用端口,以連接來自docker容器和外部容器的服務。

例:

讓我們假設您的IP為192.168.0.123那么您可以從docker容器中的任何服務甚至從外部docker以192.168.0.123:25192.168.0.123:587連接到php 這是因為您已通過將端口25,587映射到主機端口25,587暴露它們。

一些參考:

  1. Docker網絡
  2. 使用主機網絡聯網
  3. 將容器連接到用戶定義的網橋
  4. 與獨立容器聯網
  5. 服務發現
  6. 撰寫網絡 (請選中“指定自定義網絡”部分)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM