/etc/nginx/conf.d/nginx.conf中未找到Docker Nginx Django [emerg]主机

shyt4zoc  于 2023-08-03  发布在  Nginx
关注(0)|答案(2)|浏览(114)

我是Web应用程序领域的新手,只是试图在Red Hat Linux Enterprise 8.7上部署一个基本的Django应用程序,其中包括Gunicorn,Nginx和podman(版本4.4.1)。Nginx的Dockerfile是Docker Hub v 1.25.1的官方镜像。

服务器上没有docker-compose/podman-compose。

我通过创建专用网络开始构建:
第一个月
下一个组件是Django应用程序:
podman build -t testapp-django -f src/testapp-django/compose/django/Dockerfile .
应用程序的Dockerfile基于Ubuntu基础映像,我将暴露端口8000:

FROM ubuntu:22.04
...
RUN addgroup --system django \
    && adduser --system --ingroup django django
...
WORKDIR /app
...
RUN chmod +x /app
...
EXPOSE 8000
...
COPY src/testapp-django/compose/django/entrypoint /entrypoint
RUN sed -i -e 's/^M$//' /entrypoint
RUN chmod +x /entrypoint
RUN chown django /entrypoint

COPY src/testapp-django/compose/django/start /start
RUN sed -i -e 's/^M$//' /start
RUN chmod +x /start
RUN chown django /start

RUN chown -R django:django /app

USER django

ENTRYPOINT ["/entrypoint"]

字符串
/entrypoint:

set -o errexit
set -o pipefail
set -o nounset

exec "$@"


/start:

set -o errexit
set -o pipefail
set -o nounset

python3 /app/manage.py migrate
gunicorn testapp.wsgi:application --bind 0.0.0.0:8000 --chdir=/app


启动Django应用程序成功:
podman run -d -p 8010:8000 --name testapp-django --env-file src/testapp-django/.env --network testapp-net testapp-django /start
回复:

[2023-07-07 10:23:41 +0000] [24] [INFO] Starting gunicorn 20.1.0
[2023-07-07 10:23:41 +0000] [24] [INFO] Listening at: http://0.0.0.0:8000 (24)
[2023-07-07 10:23:41 +0000] [24] [INFO] Using worker: sync
[2023-07-07 10:23:41 +0000] [26] [INFO] Booting worker with pid: 26


下一步,我想启动Nginx。
Dockerfile:

FROM nginx:1.25.1

RUN rm /etc/nginx/conf.d/default.conf
COPY src/testapp-django/compose/nginx/nginx.conf /etc/nginx/conf.d


我的nginx.conf文件:

upstream testapp-django {
    server testapp-django:8000;
}

server {
    listen 80;
    location / {
        proxy_pass http://testapp-django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 20M;
    }
}


podman build -t testapp-nginx -f src/testapp-django/compose/nginx/Dockerfile .
当我运行容器时:
podman run -p 1337:80 --name testapp-nginx --network testapp-net testapp-nginx
我得到了如下的回应:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/07/07 14:48:34 [emerg] 1#1: host not found in upstream "testapp-django:8000" in /etc/nginx/conf.d/nginx.conf:2
nginx: [emerg] host not found in upstream "testapp-django:8000" in /etc/nginx/conf.d/nginx.conf:2


我在SO上的类似帖子中寻找解决方案,但没有任何成功。
检查应用程序容器,我看到以下网络:

"NetworkSettings": {
       "EndpointID": "",
       "Gateway": "",
       "IPAddress": "",
       "IPPrefixLen": 0,
       "IPv6Gateway": "",
       "GlobalIPv6Address": "",
       "GlobalIPv6PrefixLen": 0,
       "MacAddress": "",
       "Bridge": "",
       "SandboxID": "",
       "HairpinMode": false,
       "LinkLocalIPv6Address": "",
       "LinkLocalIPv6PrefixLen": 0,
       "Ports": {
            "8000/tcp": [
                 {
                      "HostIp": "",
                      "HostPort": "8000"
                 }
            ]
       },
       "SandboxKey": "/run/user/1632100669/netns/netns-10b5a628-1e92-a4ac-1800-2957e0edaf1c",
       "Networks": {
            "testapp-net": {
                 "EndpointID": "",
                 "Gateway": "10.89.1.1",
                 "IPAddress": "10.89.1.17",
                 "IPPrefixLen": 24,
                 "IPv6Gateway": "",
                 "GlobalIPv6Address": "",
                 "GlobalIPv6PrefixLen": 0,
                 "MacAddress": "16:cb:3d:2a:d1:43",
                 "NetworkID": "testapp-net",
                 "DriverOpts": null,
                 "IPAMConfig": null,
                 "Links": null,
                 "Aliases": [
                      "2a14008a1c9d"
                 ]
            }
       }
  }


在nginx容器上也是一样:

"NetworkSettings": {
   "EndpointID": "",
   "Gateway": "",
   "IPAddress": "",
   "IPPrefixLen": 0,
   "IPv6Gateway": "",
   "GlobalIPv6Address": "",
   "GlobalIPv6PrefixLen": 0,
   "MacAddress": "",
   "Bridge": "",
   "SandboxID": "",
   "HairpinMode": false,
   "LinkLocalIPv6Address": "",
   "LinkLocalIPv6PrefixLen": 0,
   "Ports": {
        "80/tcp": [
             {
                  "HostIp": "",
                  "HostPort": "1337"
             }
        ]
   },
   "SandboxKey": "",
   "Networks": {
        "testapp-net": {
             "EndpointID": "",
             "Gateway": "",
             "IPAddress": "",
             "IPPrefixLen": 0,
             "IPv6Gateway": "",
             "GlobalIPv6Address": "",
             "GlobalIPv6PrefixLen": 0,
             "MacAddress": "",
             "NetworkID": "testapp-net",
             "DriverOpts": null,
             "IPAMConfig": null,
             "Links": null,
             "Aliases": [
                  "a4a11b846dbc"
             ]
        }
   }
}

x8goxv8g

x8goxv8g1#

作为一个测试,你能试着给予你的上游一个其他的名字吗?
我的意思是编辑你的nginx.conf,让你的docker容器和上游有不同的标识符:

upstream testapp-django-upstream {
    server testapp-django:8000;
}

server {
    listen 80;
    location / {
        proxy_pass http://testapp-django-upstream;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 20M;
    }
}

字符串

4zcjmb1e

4zcjmb1e2#

通过先创建一个pod来设法使其工作:

podman pod create --name testapp [OTHER OPTIONS]

字符串
然后所有容器都使用--pod testapp选项创建:

podman container create --pod testapp --name testapp-django [OTHER OPTIONS]


这样Nginx就可以看到Django应用程序。
之后我遇到了其他问题,这些问题与在无根模式下运行时的权限有关,但在这篇文章之后设法解决了这些问题:https://www.redhat.com/sysadmin/container-permission-denied-errors

相关问题