unix 在上游找不到主机错误由Docker-compose引起

6rqinv9w  于 2022-11-23  发布在  Unix
关注(0)|答案(1)|浏览(168)

tldr版本:我不知道是什么导致了这个错误。我很肯定这不是行尾,因为我用记事本++手动更改了它们(除非我需要更改的不仅仅是entrypoint.sh,因为这就是我更改的所有行尾)。

原帖如下。

我不知道是什么原因导致了这个错误,当我在命令行中执行docker-compose -f docker-compose-deploy.yml up --build时,我得到了以下输出

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Starting mygoattheend-copy_app_1 ... done
Starting mygoattheend-copy_proxy_1 ... done
Attaching to mygoattheend-copy_app_1, mygoattheend-copy_proxy_1
app_1    | exec /scripts/entrypoint.sh: no such file or directory
proxy_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
proxy_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
proxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
proxy_1  | 10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
proxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
proxy_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
proxy_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
mygoattheend-copy_app_1 exited with code 1
proxy_1  | 2022/11/03 18:51:39 [emerg] 1#1: host not found in upstream "app" in /etc/nginx/conf.d/default.conf:9
proxy_1  | nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:9
mygoattheend-copy_proxy_1 exited with code 1

搜索***nginx时出现的其他错误示例:[emerg]在/etc/nginx/conf.d/default.conf中的上游“app”中找不到主机:9***联机提示该问题是由于缺少-depends_on:所以我在下面包含了我的docker-compose文件,但是我完全按照教程来做,他的工作很好。我的docker-compose-deploy有它的-depends_on:
我完整的Docker作曲如下

version: '3.7'

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: sh -c "python manage.py runserver 0.0.0.0:8000"
    environment:
      - DEBUG=1

我的完整docker-compose-deploy.yml如下

version: '3.7'

services:
  app:
    build:
      context: .
    volumes:
      - static_data:/vol/web
    environment:
      - SECRET_KEY=samplesecretkey123
      - ALLOWED_HOSTS=127.0.0.1,localhost
  proxy:
    build:
      context: ./proxy
    volumes:
      - static_data:/vol/static
    ports:
      - "8080:8080"
    depends_on:
      - app

volumes:
  static_data:

下面的图片是我的完整目录

该错误确实提到,它无法找到应用程序,我复制与主要的dockerfile(而不是在代理文件夹)
我的主dockerfile如下。

FROM python:3.8-alpine
ENV PATH="/scripts:${PATH}"
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN pip install -r /requirements.txt
RUN apk del .tmp
RUN mkdir /app
COPY ./app /app
WORKDIR /app
COPY ./scripts /scripts
RUN chmod +x /scripts/*
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/
RUN adduser -D user
RUN chown -R user:user /vol
RUN chmod -R 755 /vol/web
USER user
CMD ["entrypoint.sh"]

什么可能导致此错误?
你还需要什么信息来解决这个问题?
我在下面这个教程。我在最后https://www.youtube.com/watch?v=nh1ynJGJuT8

更新1(添加额外信息)

我代理/default.conf在下面

server {
  listen 8080;
  location /static {
    alias /vol/static;
  }
  location / {
    uwsgi_pass app:8000;
    include /etc/nginx/uwsgi_params;
  }
}

我的代理/文档文件在下面

FROM nginxinc/nginx-unprivileged:1-alpine

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

USER root

RUN mkdir -p /vol/static
RUN chmod 755 /vol/static

USER nginx
更新2

这是我上传到github https://github.com/tgmjack/help的整个项目

更新3

在vscode中编辑行尾似乎不起作用。

更新4

新的dockerfile正在尝试dos 2unix

FROM python:3.8-alpine
ENV PATH="/scripts:${PATH}"
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers dos2unix
RUN pip install -r /requirements.txt
RUN apk del .tmp
RUN mkdir /app
COPY ./app /app
WORKDIR /app
COPY ./scripts /scripts
RUN chmod +x /scripts/*
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/
RUN adduser -D user
RUN chown -R user:user /vol
RUN chmod -R 755 /vol/web
USER user
CMD ["dos2unix", "entrypoint.sh"]

但是我还是得到同样的错误。

更新5

好的,我entrypoint.sh用记事本++手动更改了www.example.com的EOL,但是我仍然得到同样的错误。我需要将此应用到不仅仅是entrypoint.sh?

k10s72fa

k10s72fa1#

此设置中存在两个问题。

DOS行尾

第一个问题是在入口点使用DOS行尾。下面是我使用od得到文件的逐字符转储时看到的结果:

$ od -c scripts/entrypoint.sh | head -n 2
0000000   #   !   /   b   i   n   /   s   h  \r  \n  \r  \n   s   e   t
0000020       -   e  \r  \n  \r  \n   p   y   t   h   o   n       m   a

问题是在#!/bin/sh之后,我们有了\r\n,而我们应该只有\n。这是DOS风格的行结尾,但Linux只需要一个\nMore information
我使用了一个名为dos 2unix的程序来替换这些行:

$ dos2unix scripts/entrypoint.sh 
dos2unix: converting file scripts/entrypoint.sh to Unix format...

(VS代码也可以这样做-here's how.
当我运行它时,我得到一个新的错误:

app_1    | Traceback (most recent call last):
app_1    |   File "manage.py", line 21, in <module>
app_1    |     main()
app_1    |   File "manage.py", line 17, in main
app_1    |     execute_from_command_line(sys.argv)
app_1    |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
app_1    |     utility.execute()
app_1    |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 345, in execute
app_1    |     settings.INSTALLED_APPS
app_1    |   File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
app_1    |     self._setup(name)
app_1    |   File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
app_1    |     self._wrapped = Settings(settings_module)
app_1    |   File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 142, in __init__
app_1    |     mod = importlib.import_module(self.SETTINGS_MODULE)
app_1    |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
app_1    |     return _bootstrap._gcd_import(name[level:], package, level)
app_1    |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
app_1    |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
app_1    |   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
app_1    |   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
app_1    |   File "<frozen importlib._bootstrap_external>", line 843, in exec_module
app_1    |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
app_1    |   File "/app/app/settings.py", line 31, in <module>
app_1    |     ALLOWED_HOSTS.extend(ALLOWED_HOSTS.split(',')) # .split(",") = incase multiple so we seperate them with a comma
app_1    | AttributeError: 'list' object has no attribute 'split'

设置文件中引用了错误的变量

我编辑了文件app/app/settings.py,并更改了以下行

ALLOWED_HOSTS.extend(ALLOWED_HOSTS.split(','))

ALLOWED_HOSTS.extend(ALLOWED_HOSTS_ENV.split(','))

在这之后,我发现这个工作。
恩金斯
nginx的配置没有问题。问题是应用程序死了,所以nginx无法进行DNS查找来找到应用程序容器的IP地址。修复应用程序容器也就修复了nginx。

相关问题