了解 Docker 使用 Docker-Alpine-Python-Flask 编写 nginx-proxy



我在使用 Docker 时遇到了一些问题。 我的问题是在使用 docker-compose 和这个库时理解反向代理 https://github.com/jwilder/nginx-proxy

所以我有 1 个 Dockerfile 像,在这里:

FROM alpine:latest
COPY requirements.txt /tmp/requirements.txt
RUN apk add --no-cache 
g++ 
gcc 
freetds 
ca-certificates 
build-base 
libc-dev 
python3 
python3-dev 
unixodbc-dev 
bash 
nginx 
uwsgi 
uwsgi-python3 
supervisor && 
python3 -m ensurepip && 
rm -r /usr/lib/python*/ensurepip && 
pip3 install --no-cache-dir --upgrade --force-reinstall pip setuptools && 
pip3 install -r /tmp/requirements.txt && 
rm /etc/nginx/conf.d/default.conf && 
rm -r /root/.cache
# Copy ODBC Driver
COPY odbc.ini /etc/
COPY odbcinst.ini /etc/
# Copy the Nginx global conf
COPY nginx.conf /etc/nginx/
# Copy the Flask Nginx site conf
COPY flask-site-nginx.conf /etc/nginx/conf.d/
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
COPY uwsgi.ini /etc/uwsgi/
# Custom Supervisord config
COPY supervisord.conf /etc/supervisord.conf
# Add demo app
COPY ./app /app
# EXPOSE 80
WORKDIR /app
CMD ["/usr/bin/supervisord"]

当我跑步时

docker build -t name/name . #success
docker run -d --name=container_name -p 80 name/name:latest #success

我跑了

curl -i localhost/page #success

------------,我的问题是在使用nginx代理------------------时理解 在分配的端口内

1 创建 docker 网络名称反向代理并运行 jwilder/nginx-proxy

docker network create --driver bridge reverse-proxy
docker run -d -p 81:80 -p 444:443 
--name reverse-proxy 
--net=reverse-proxy 
-v /var/run/docker.sock:/tmp/docker.sock:ro 
jwilder/nginx-proxy

这是我的码头工人-作曲.yml

version: "3"
services:
atk-request:
build: ./_atkrequest                 #path which Dockerfile is exists
container_name: api-atkrequest        
restart: always
networks: 
- reverse-proxy
# ports:
#   - 80:80                            #host:container
expose:
- 80
environment:
- VIRTUAL_HOST=atkrequest.api.dev  #nginx-proxy host
- VIRTUAL_PORT=80                  #nginx-proxy port
networks: 
reverse-proxy:
external: 
name: reverse-proxy

然后运行

docker-compose up -d #success without error

和输出码头工人 PS

CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                      NAMES
ca95a0f42de2        atkrequest_atk-request   "/usr/bin/supervisord"   5 seconds ago        Up 3 seconds        80/tcp                                     api-atkrequest
c42a9f9d85b5        jwilder/nginx-proxy      "/app/docker-entrypoâ¦"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   reverse-proxy

以及当调用带有 curl 错误代码 503 服务的 URL 时 服务暂时不可用

curl -i localhost
HTTP/1.1 503 Service Temporarily Unavailable
Server: nginx/1.14.0
Date: Wed, 10 Oct 2018 07:39:09 GMT
Content-Type: text/html
Content-Length: 213
Connection: keep-alive
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body bgcolor="white">
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

然后我被困在这里...

谢谢 益

jwilder/nginx-proxy生成一个 Nginxdefault.conf,其中包含每个已定义VIRTUAL_HOST容器的条目。

由于已在其中一个容器上定义了VIRTUAL_HOST=atkrequest.api.dev,因此生成的default.conf将具有类似

# atkrequest.api.dev
upstream atkrequest.api.dev {
## Can be connected with "{{dir_name}}_default" network
# {{dir_name}}_atk-request
server {{atk-request container ip}}:80;
}
server {
server_name atkrequest.api.dev;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location / {
proxy_pass http://atkrequest.api.dev;
}
}

但是不会为localhost配置任何内容,因为容器VIRTUAL_HOST=localhost没有定义。因此,当您的请求到达nginx-proxy时 - 它会以503响应,因为它没有任何localhost配置。

如果您想保持nginx-proxy并能够使用localhost访问atk-request容器 - 将其环境变量更改为VIRTUAL_HOST=localhost。(矫枉过正的技术,因为您可以简单地公开端口80:80而不使用nginx-proxy,但我将其包含在解释中(

如果您想使用atkrequest.api.dev访问atk-request容器 - 只需将127.0.0.1 atkrequest.api.dev添加到您的/etc/hosts

最新更新