将 docker 镜像部署到 heroku

时间:2021-01-12 21:21:00

标签: docker nginx heroku heroku-cli

我有 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 不支持它们。

1 个答案:

答案 0 :(得分:0)

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

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

您可以查看不同的选项:

  1. 使用 buildpack 而不是 Docker:Customizing Web Server and Runtime Settings for PHP
  2. 使用 PHP 和 NGINX 创建单个镜像(DockerHub 有一些)
  3. 在 fastcgi_pass 配置中使用 PHP 应用程序 dyno (https://php.herokuapp.com) 的 URL