首页 > 解决方案 > 我的 docker-compose.yml 文件应该如何将 HAProxy 与 Heroku 和 Docker 一起使用?

问题描述

我试图关注这篇文章,但现在很多事情看起来都不一样了,所以我最终即兴创作。我想简单地将前端与后端连接起来,避免 CORS 头痛(尽管现在我不确定哪个头痛更大)。无论如何,我以这种方式定义了我的负载均衡器:

应用程序.json:

{
  "name": "NeuroCore Load Balancer",
  "description": "A load balancer for NeuroCore"
}

档案:

web: sbin/haproxy -f haproxy.cfg

Dockerfile:

FROM heroku/heroku:18

RUN mkdir -p /app/user  
WORKDIR /app/user

# Install HAProxy

RUN apt-get update && apt-get install -y libssl1.0.0 libpcre3 --no-install-recommends && rm -rf /var/lib/apt/lists/*

ENV HAPROXY_MAJOR 1.5  
ENV HAPROXY_VERSION 1.5.14  
ENV HAPROXY_MD5 ad9d7262b96ba85a0f8c6acc6cb9edde

# see http://sources.debian.net/src/haproxy/1.5.8-1/debian/rules/ for some helpful navigation of the possible "make" arguments
RUN buildDeps='curl gcc libc6-dev libpcre3-dev libssl-dev make' \  
    && set -x \
    && apt-get update && apt-get install -y $buildDeps --no-install-recommends && rm -rf /var/lib/apt/lists/* \
    && curl -SL "http://www.haproxy.org/download/${HAPROXY_MAJOR}/src/haproxy-${HAPROXY_VERSION}.tar.gz" -o haproxy.tar.gz \
    && echo "${HAPROXY_MD5}  haproxy.tar.gz" | md5sum -c \
    && mkdir -p /app/user/src/haproxy \
    && tar -xzf haproxy.tar.gz -C /app/user/src/haproxy --strip-components=1 \
    && rm haproxy.tar.gz \
    && make -C /app/user/src/haproxy \
        TARGET=linux2628 \
        USE_PCRE=1 PCREDIR= \
        USE_OPENSSL=1 \
        USE_ZLIB=1 \
    PREFIX=/app/user \
        all \
        install-bin \
    && rm -rf /app/user/src/haproxy \
    && apt-get purge -y --auto-remove $buildDeps

COPY haproxy.cfg /app/user/haproxy.cfg

haproxy.cfg:

global  
    maxconn 256

defaults  
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http  
    bind 0.0.0.0:$PORT

    option forwardfor

    # Force SSL
    redirect scheme https code 301 if ! { hdr(x-forwarded-proto) https }

    # And all other requests to `example-com`.
    default_backend neurocore-backend

backend neurocore-backend  
    http-request set-header X-Forwarded-Host neurocore.herokuapp.com
    http-request set-header X-Forwarded-Port %[dst_port]

    reqirep ^Host: Host:\ neurocore-backend.herokuapp.com    

    server backend neurocore-backend.herokuapp.com:443 ssl verify none

运行后docker-compose up web我得到:

ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml

据我了解,这个文件应该是由heroku docker:init命令生成的,但现在它不起作用。那么,我应该怎么做才能让我的应用程序启动并运行呢?

标签: dockerherokudocker-composehaproxy

解决方案


推荐阅读