首页 > 解决方案 > 应用程序崩溃并未能绑定 $PORT 60 秒

问题描述

我在 Heroku 中的应用程序有问题,我的设置是 Nginx、Docker 和 Php,以及一个简单的 Php 脚本。在 Heroku CLI 中发出发布命令并打开应用程序后,我的应用程序在浏览器中启动但它崩溃了。

这是日志。

2020-05-30T15:46:38.733789+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-30T15:46:51.620126+00:00 heroku[web.1]: Starting process with command `nginx -g daemon\ off\;`
2020-05-30T15:47:52.178537+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-05-30T15:47:52.210903+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-05-30T15:47:52.317640+00:00 heroku[web.1]: Process exited with status 137
2020-05-30T15:47:52.373907+00:00 heroku[web.1]: State changed from starting to crashed

Dockerfile

FROM  ubuntu:latest

RUN apt-get update \
    && apt-get install -y curl zip unzip  \
            php7.4 php7.4-fpm \
            nginx


COPY ./webapp /var/www/myapp

WORKDIR /var/www/myapp

# ADD default.conf /etc/nginx/conf.d/default.conf

COPY default.conf.template /etc/nginx/conf.d/default.conf.template
COPY nginx.conf /etc/nginx/nginx.conf

# for ubuntu
RUN useradd -m myuser
USER myuser


CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'

更新

按照评论中的建议添加端口后,现在出现新错误

/bin/bash: envsubst: 找不到命令

标签: phpdockernginxdockerfile

解决方案


正如您在 Dockerfile 中提到的那样CMD$PORT您必须在 Dockerfile 中或在运行容器时传递该环境变量。

CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'

如果你想在 Dockerfile 中提供,

FROM  ubuntu:latest
ENV PORT=80 # Add this line
...
...
CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'

或者如果您想在运行时覆盖,请使用docker run命令传递它

docker run -e PORT=8008 imageName

推荐阅读