运行Django+ PostgreSql+使用compose -File许可错误在Docker容器中的Gunicorn

发布于 2025-02-05 18:16:30 字数 6523 浏览 2 评论 0原文

我正在使用docker-desktop版本20.10.16在Ubuntu 20.0.4 Lts上

我想dockerise django(v3.2),PostgreSQL(v14.3)和Gunicorn(v20.1.0)(v20.1.0)(v20.1.0)。并将这些容器用作由我本地计算机上运行的单个NGINX实例服务的虚拟主机。

我能够构建图像和一个容器,但是设置Django应用程序失败了,因为在创建/写入myporoj.log时会出现文件错误 我目前是,不使用nginx作为虚拟主机的代理服务器 - 我希望o在继续使用Nginx与我的设置中使用Nginx之前先解决此ISSE。

以下是其他相关详细信息:

我的文件结构(在本地计算机上)如下:

/path/to/myprojectweb/
            /myproj
               /myproj
               settings.py 
               wsgi.py
               # etc.
           Dockerfile.prod
           compose.prod.yml

dockerfile.prod.yml

# Builder Image

FROM python:3.8-slim-buster as Builder


WORKDIR /usr/src/app


ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWEITEBYTECODE 1


RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev
  

RUN pip install --upgrade pip pipenv
COPY Pipfile* ./
RUN pipenv lock --keep-outdated --requirements > requirements.txt \
    && pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


# Final Image

FROM python:3.8-slim-buster

RUN mkdir -p /home/app  /var/log/gunicorn

# create user + group app that apps will run as.
RUN groupadd -r app && useradd --no-log-init -r -g app app

ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME


RUN apt-get update && apt-get install -y libpq-dev
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt ./

RUN chown -R app:app $HOME
RUN chown -R app:app $APP_HOME && chmod ug+rws $APP_HOME
RUN chown -R app:app /var/log/gunicorn

# change user before install, so 3rd party packages can be updated with makemigrations/migrate
USER app
RUN pip install --no-cache /wheels/*

COPY . $APP_HOME

compose.prod.yml

services:

  db:
    image: postgres:14.3

    restart: always
    container_name: myproj_postgres_db
    env_file:
      - .env

    volumes:
      - postgres_data:/var/lib/postgresql/data/

  # Gunicorn tips from: https://pythonspeed.com/articles/gunicorn-in-docker/
  web:
    build: 
      context: .
      dockerfile: Dockerfile.prod
    container_name: myproj_app
    restart: always
    command: >
      sh -c "python manage.py makemigrations && python manage.py migrate &&
             gunicorn --capture-output \
                      --access-logfile /var/log/gunicorn/access.log  \
                      --error-logfile /var/log/gunicorn/error.log  \
                      --enable-stdio-inheritance  \
                      --workers 3  \
                      --worker-tmp-dir /dev/shm \
                      --bind 0.0.0.0:8000 \
                       myproj.wsgi:application" 
    #--bind unix:/var/log/gunicorn/myproj.sock 
    
    ports:
      - 8000:8000
    env_file:
      - .env

    depends_on:
      - db

volumes:
  postgres_data:      

这是以下命令的输出(shippet):

/path/to/mypjo/$ sudo docker compose -f compose.prod.yml up -build

myproj_app          | Traceback (most recent call last):
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 563, in configure
myproj_app          |     handler = self.configure_handler(handlers[name])
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 744, in configure_handler
myproj_app          |     result = factory(**kwargs)
myproj_app          |   File "/usr/local/lib/python3.8/logging/handlers.py", line 200, in __init__
myproj_app          |     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
myproj_app          |   File "/usr/local/lib/python3.8/logging/handlers.py", line 55, in __init__
myproj_app          |     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
myproj_app          |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1147, in __init__
myproj_app          |     StreamHandler.__init__(self, self._open())
myproj_app          |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1176, in _open
myproj_app          |     return open(self.baseFilename, self.mode, encoding=self.encoding)
myproj_app          | PermissionError: [Errno 13] Permission denied: '/home/app/web/myproj.log'
myproj_app          | 
myproj_app          | The above exception was the direct cause of the following exception:
myproj_app          | 
myproj_app          | Traceback (most recent call last):
myproj_app          |   File "manage.py", line 22, in <module>
myproj_app          |     main()
myproj_app          |   File "manage.py", line 18, in main
myproj_app          |     execute_from_command_line(sys.argv)
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
myproj_app          |     utility.execute()
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
myproj_app          |     django.setup()
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
myproj_app          |     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/utils/log.py", line 75, in configure_logging
myproj_app          |     logging_config_func(logging_settings)
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 808, in dictConfig
myproj_app          |     dictConfigClass(config).configure()
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 570, in configure
myproj_app          |     raise ValueError('Unable to configure handler '
myproj_app          | ValueError: Unable to configure handler 'logfile'
myproj_app          | sh: 3: --access-logfile: not found
myproj_app          | sh: 4: --error-logfile: not found
myproj_app          | sh: 5: --enable-stdio-inheritance: not found
myproj_app          | sh: 6: --workers: not found
myproj_app          | sh: 7: --worker-tmp-dir: not found
myproj_app          | sh: 8: --bind: not found
myproj_app          | sh: 9: myproj.wsgi:application: not found
myproj_app exited with code 127

- 虽然我可以使用所需文件夹的权限(甚至在适当的情况下设置粘性标志) - 我仍然有文件许可错误,而gunicorn也barfs barfs barfs基因找不到wsgi.py

如何修复文件错误权限问题,还要让Gunicorn运行(使用我通过CL标志传递给它的配置数据?)

I am using Docker-desktop version version 20.10.16 on Ubuntu 20.0.4 LTS

I want to Dockerise Django (v3.2), PostgreSQl (v14.3) and Gunicorn (v20.1.0) in a single container. And use those containers as Virtual hosts being serviced by a single Nginx instance that is running on my local machine.

I am able to build images, and a container, but setting up the django application fails, because of a file error in creating/writing to myporoj.log
I am currently, not using nginx as a proxy server to the virtual hosts - I want o resolve this isse first before moving on to using Nginx with my setup.

Here are additional relevant details:

My file structure (on local machine), is as follows:

/path/to/myprojectweb/
            /myproj
               /myproj
               settings.py 
               wsgi.py
               # etc.
           Dockerfile.prod
           compose.prod.yml

Dockerfile.prod.yml

# Builder Image

FROM python:3.8-slim-buster as Builder


WORKDIR /usr/src/app


ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWEITEBYTECODE 1


RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev
  

RUN pip install --upgrade pip pipenv
COPY Pipfile* ./
RUN pipenv lock --keep-outdated --requirements > requirements.txt \
    && pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


# Final Image

FROM python:3.8-slim-buster

RUN mkdir -p /home/app  /var/log/gunicorn

# create user + group app that apps will run as.
RUN groupadd -r app && useradd --no-log-init -r -g app app

ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME


RUN apt-get update && apt-get install -y libpq-dev
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt ./

RUN chown -R app:app $HOME
RUN chown -R app:app $APP_HOME && chmod ug+rws $APP_HOME
RUN chown -R app:app /var/log/gunicorn

# change user before install, so 3rd party packages can be updated with makemigrations/migrate
USER app
RUN pip install --no-cache /wheels/*

COPY . $APP_HOME

compose.prod.yml

services:

  db:
    image: postgres:14.3

    restart: always
    container_name: myproj_postgres_db
    env_file:
      - .env

    volumes:
      - postgres_data:/var/lib/postgresql/data/

  # Gunicorn tips from: https://pythonspeed.com/articles/gunicorn-in-docker/
  web:
    build: 
      context: .
      dockerfile: Dockerfile.prod
    container_name: myproj_app
    restart: always
    command: >
      sh -c "python manage.py makemigrations && python manage.py migrate &&
             gunicorn --capture-output \
                      --access-logfile /var/log/gunicorn/access.log  \
                      --error-logfile /var/log/gunicorn/error.log  \
                      --enable-stdio-inheritance  \
                      --workers 3  \
                      --worker-tmp-dir /dev/shm \
                      --bind 0.0.0.0:8000 \
                       myproj.wsgi:application" 
    #--bind unix:/var/log/gunicorn/myproj.sock 
    
    ports:
      - 8000:8000
    env_file:
      - .env

    depends_on:
      - db

volumes:
  postgres_data:      

Here is the output (snippet) from the following command:

/path/to/mypjo/$ sudo docker compose -f compose.prod.yml up --build

myproj_app          | Traceback (most recent call last):
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 563, in configure
myproj_app          |     handler = self.configure_handler(handlers[name])
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 744, in configure_handler
myproj_app          |     result = factory(**kwargs)
myproj_app          |   File "/usr/local/lib/python3.8/logging/handlers.py", line 200, in __init__
myproj_app          |     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
myproj_app          |   File "/usr/local/lib/python3.8/logging/handlers.py", line 55, in __init__
myproj_app          |     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
myproj_app          |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1147, in __init__
myproj_app          |     StreamHandler.__init__(self, self._open())
myproj_app          |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1176, in _open
myproj_app          |     return open(self.baseFilename, self.mode, encoding=self.encoding)
myproj_app          | PermissionError: [Errno 13] Permission denied: '/home/app/web/myproj.log'
myproj_app          | 
myproj_app          | The above exception was the direct cause of the following exception:
myproj_app          | 
myproj_app          | Traceback (most recent call last):
myproj_app          |   File "manage.py", line 22, in <module>
myproj_app          |     main()
myproj_app          |   File "manage.py", line 18, in main
myproj_app          |     execute_from_command_line(sys.argv)
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
myproj_app          |     utility.execute()
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
myproj_app          |     django.setup()
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
myproj_app          |     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
myproj_app          |   File "/home/app/.local/lib/python3.8/site-packages/django/utils/log.py", line 75, in configure_logging
myproj_app          |     logging_config_func(logging_settings)
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 808, in dictConfig
myproj_app          |     dictConfigClass(config).configure()
myproj_app          |   File "/usr/local/lib/python3.8/logging/config.py", line 570, in configure
myproj_app          |     raise ValueError('Unable to configure handler '
myproj_app          | ValueError: Unable to configure handler 'logfile'
myproj_app          | sh: 3: --access-logfile: not found
myproj_app          | sh: 4: --error-logfile: not found
myproj_app          | sh: 5: --enable-stdio-inheritance: not found
myproj_app          | sh: 6: --workers: not found
myproj_app          | sh: 7: --worker-tmp-dir: not found
myproj_app          | sh: 8: --bind: not found
myproj_app          | sh: 9: myproj.wsgi:application: not found
myproj_app exited with code 127

So although I chmod permissions for required folders (even setting sticky flag where appropriate) - I still have file permission errors, and Gunicorn also barfs abut not finding wsgi.py

How do I fix the file error permission issue and also, get gunicorn to run (using the configuration data I am passing on to it through CL flags?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文