正在将 docker 个映像部署到 heroku

Deploying docker images to heroku

我有 2 张 docker 图片(一张用于 nginx,一张用于 php)。我正在使用 Docker CLI 将它们推送并发布到 docker。推送和释放操作没有错误,但是当我查看日志时,我可以看到 nginx 立即崩溃。

日志:

2021-01-12T21:01:01.531944+00:00 heroku[web.1]: Starting process with command `nginx -g daemon\ off\;`
2021-01-12T21:01:03.415460+00:00 heroku[php.1]: Starting process with command `php-fpm`
2021-01-12T21:01:04.046397+00:00 heroku[php.1]: State changed from starting to up
2021-01-12T21:01:04.169516+00:00 app[web.1]: 2021/01/12 21:01:04 [warn] 3#3: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-01-12T21:01:04.169529+00:00 app[web.1]: nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-01-12T21:01:04.222513+00:00 app[web.1]: 2021/01/12 21:01:04 [emerg] 3#3: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:16
2021-01-12T21:01:04.222514+00:00 app[web.1]: nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:16
2021-01-12T21:01:04.302526+00:00 heroku[web.1]: Process exited with status 1
2021-01-12T21:01:04.347702+00:00 heroku[web.1]: State changed from starting to crashed

Dockerfile.php

FROM php:7.4.3-fpm-alpine3.11

ENV RUN_DEPS \
    zlib \
    libzip \
    libpng \
    libjpeg-turbo \
    postgresql-libs

ENV BUILD_DEPS \
    zlib-dev \
    libzip-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    postgresql-dev

ENV PHP_EXTENSIONS \
    opcache \
    zip \
    gd \
    bcmath \
    pgsql \
    pdo_pgsql

RUN apk add --no-cache --virtual .build-deps $BUILD_DEPS \
    && docker-php-ext-configure gd --with-jpeg \
    && docker-php-ext-install -j "$(nproc)" $PHP_EXTENSIONS \
    && apk del .build-deps

RUN apk add --no-cache --virtual .run-deps $RUN_DEPS
# Copy the application code
COPY . /app

VOLUME ["/app"]

Dockerfile.web

FROM nginx:1.17.8-alpine

# Copy the public directory
COPY ./public/ /app/public/
COPY . /app/

# Copy the nginx config file
COPY ./docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf

nginx.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    server_tokens off;

    root /app/;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

老实说,我不知道为什么会这样。这是我第一次使用 Heroku 和 Docker。 我在本地使用 docker-compose 文件来构建它,但我知道 Heroku 不支持它们。

问题在这里:host not found in upstream "php"

传入的 NGINX 请求被代理到 php:9000,这在您的本地 Docker-Compose 上很好,因为 2 个容器共享同一网络。
在 Heroku 上,每个容器都是一个 Web Dyno:dynos 无法通过内部网络进行通信,但它们只接受来自外部(public 互联网)的 HTTPS 请求。

您可以查看不同的选项:

  1. 使用构建包代替 Docker:Customizing Web Server and Runtime Settings for PHP
  2. 使用 PHP 和 NGINX 创建单个图像(DockerHub 有几个)
  3. 在 fastcgi_pass 配置中使用 PHP app dyno (https://php.herokuapp.com) 的 URL