1

我正在尝试使用 Caddy 提供一个 PHP 应用程序,全部 Dockerized。这是我的球童文件:

http://mydomain.test
root * /app/public
php_fastcgi localhost:9000
rewrite * /index.php?{query}&p={path}
file_server

我的 Dockerfile:

FROM php:7.0-fpm
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/
COPY --from=caddy:2.3.0-alpine /usr/bin/caddy /usr/local/bin/
RUN apt-get update -y && apt-get install -y libpng-dev libpq-dev
RUN docker-php-ext-install gd
RUN docker-php-ext-install zip
RUN docker-php-ext-install pdo_mysql
WORKDIR /app
COPY composer.* ./
RUN composer install
COPY . ./
CMD ["caddy", "run"]

和我的 docker-compose.yml:

version: "3"

services:
  app:
    build: .
    ports:
      - 8080:80

这三个文件都在我的 PHP 项目的根目录中。但是,当我使用 docker-compose 启动它然后访问 mydomain.test 时,我得到一个 502 HTTP 状态代码。在 Docker 容器的标准输出中,我看到:

{
  "level": "error",
  "ts": 1624230589.230253,
  "logger": "http.log.error",
  "msg": "dialing backend: dial tcp 127.0.0.1:9000: connect: connection refused",
  "request": {
    "remote_addr": "192.168.112.1:54528",
    "proto": "HTTP/1.1",
    "method": "GET",
    "host": "mydomain.test:8080",
    "uri": "/",
    "headers": {
      "Upgrade-Insecure-Requests": [
        "1"
      ],
      "User-Agent": [
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
      ],
      "Accept": [
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
      ],
      "Accept-Encoding": [
        "gzip, deflate"
      ],
      "Accept-Language": [
        "en-GB,en;q=0.9,es;q=0.8,en-US;q=0.7"
      ],
      "Connection": [
        "keep-alive"
      ],
      "Cache-Control": [
        "max-age=0"
      ]
    }
  },
  "duration": 0.000814111,
  "status": 502,
  "err_id": "rm3277ykx",
  "err_trace": "reverseproxy.statusError (reverseproxy.go:783)"
}

所以从 msg 看来,Caddy 连接到 php-fpm 时出现问题。但是它们都在同一个容器中运行,所以我不明白为什么在 localhost:9000 无法访问它?

4

1 回答 1

0

您的 php-fpm 尚未运行,这是因为您试图在同一个 Dockerfile 中启动 caddy。您需要在 docker-compose 文件中拆分 caddy 和 php-fpm。我让它与以下配置一起工作。

球童文件:

example.com {
    root * /app/public
    php_fastcgi php-fpm:9000
    file_server
}

码头工人-compose.yml:

version: '3.3'
services:
  caddy:
    container_name: caddy
    image: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - /home/websites:/app/public
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - php-fpm
  php-fpm:
    container_name: php-fpm
    build: .
    restart: unless-stopped
    volumes:
      - /home/websites:/app/public

volumes:
  caddy_data:
  caddy_config:

networks:
  default:
    external:
      name: main

Dockerfile:

FROM php:7.0-fpm

RUN apt-get update -y
RUN docker-php-ext-install pdo_mysql
于 2021-08-04T16:30:17.630 回答