【问题标题】:How to expose a port in heroku?如何在heroku中公开一个端口?
【发布时间】:2020-07-21 22:11:07
【问题描述】:

所以我在 heroku 上部署了一个应用程序,部署后,它说它在 localhost:5000 上运行。我想使用 $PORT 变量公开这个端口。执行此操作的确切命令是什么?这是相关的:https://devcenter.heroku.com/articles/container-registry-and-runtime#dockerfile-commands-and-runtime,但是如何真正公开端口?这就是我的 Dockerfile 的样子:

# We will use Ubuntu for our image
FROM ubuntu:latest

# Updating Ubuntu packages

ARG CLOJURE_TOOLS_VERSION=1.10.1.507


RUN apt-get -qq update && apt-get -qq -y install curl wget bzip2 openjdk-8-jdk-headless \
    && curl -sSL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -o /tmp/miniconda.sh \
    && bash /tmp/miniconda.sh -bfp /usr/local \
    && rm -rf /tmp/miniconda.sh \
    && conda install -y python=3 \
    && conda update conda \
    && curl -o install-clojure https://download.clojure.org/install/linux-install-${CLOJURE_TOOLS_VERSION}.sh \
    && chmod +x install-clojure \
    && ./install-clojure && rm install-clojure \
    && wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein \
    && chmod a+x lein \
    && mv lein /usr/bin \
    && apt-get -qq -y autoremove \
    && apt-get autoclean \
    && rm -rf /var/lib/apt/lists/* /var/log/dpkg.log \
    && conda clean --all --yes


ENV PATH /opt/conda/bin:$PATH

RUN conda create -n pyclj python=3.6 && conda install -n pyclj numpy mxnet

## To install pip packages into the pyclj environment do
RUN conda run -n pyclj python3 -mpip install numpy
#COPY . .
RUN lein uberjar
RUN java $JVM_OPTS -cp target/myapp.jar clojure.main -m myapp.application --server.port=$PORT

https://github.com/heroku/alpinehelloworld/blob/master/Dockerfile 此处的 Dockerfile 示例显示了 gunicorn 命令,但我的项目到目前为止还没有使用 gunicorn。如何配置我的 Dockerfile 以便我的应用程序使用 $PORT 环境变量?

我正在使用 aleph.io:

(ns myapp.application
  (:gen-class)
  (:require ;; more requires 
              [system.components.aleph :refer [new-web-server]]
            ))

(defn app-system [config]
  (component/system-map
   :routes     (new-endpoint home-routes)
   :middleware (new-middleware {:middleware (:middleware config)})
   :handler    (-> (new-handler :router :bidi
                    )
                   (component/using [:routes :middleware]))
   :http       (-> (new-web-server (:http-port config) :handler)
                   (component/using [:handler]))
   :server-info (server-info (:http-port config))))

(defn -main [& _]
  (let [config (config)]
    (-> config
        app-system
        component/start)))

【问题讨论】:

    标签: docker heroku


    【解决方案1】:

    日志条目"(...) when deployed, it says it's running on localhost:5000" 指出监听主机被限制为localhost。因此无法从 Internet 访问您的服务器(或使用 heroku 域)。

    与您使用的服务器无关,您需要将监听主机设置为0.0.0.0。在不知道您的服务器框架的情况下,我假设您必须编辑 docker 文件中的最后一行(并添加 --server.host=0.0.0.0):

    RUN java $JVM_OPTS -cp target/myapp.jar clojure.main -m myapp.application --server.port=$PORT --server.host=0.0.0.0
    

    【讨论】:

    • 在最后一个命令后面加上 --server.host=0.0.0.0 在推送期间在远程日志中显示相同:Started myapp on localhost:5000
    • 您的应用使用什么(网络)服务器框架?
    • 我正在使用aleph.io(defn app-system [config] (component/system-map :routes (new-endpoint home-routes) :middleware (new-middleware {:middleware (:middleware config)}) :handler (-> (new-handler :router :bidi ) (component/using [:routes :middleware])) :http (-> (new-web-server (:http-port config) :handler) (component/using [:handler])) :server-info (server-info (:http-port config)))) 我的编辑具有几乎相同的代码,但格式更好。
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多