问题描述
我有 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 不能通过内部网络进行通信,但它们只接受来自外部(公共互联网)的 HTTPS 请求。
您可以查看不同的选项:
- 使用 buildpack 而不是 Docker:Customizing Web Server and Runtime Settings for PHP
- 使用 PHP 和 NGINX 创建单个镜像(DockerHub 有一些)
- 在 fastcgi_pass 配置中使用 PHP 应用程序 dyno (https://php.herokuapp.com) 的 URL