docker 带有 django gunicorn nginx 的容器
docker container with django gunicorn nginx
我正在尝试使用 gunicorn 和 nginx 将 django docker化为 docker 图像。来自 this tutorial
运行 docker-compose up --detach --build
一切构建成功。但是 gunicorn 没有启动。错误日志显示:
bash: /home/soccer/venv/bin/gunicorn:
/Users/Umar/PycharmProjects/soccer/venv/bin/python: bad
interpreter: No such file or directory
我该如何解决这个问题?
Dockerfile:
# pull official base image
FROM python:3.7
# accept arguments
ARG PIP_REQUIREMENTS=production.txt
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install --upgrade pip setuptools
# create user for the Django project
RUN useradd -ms /bin/bash soccer
# set current user
USER soccer
# set work directory
WORKDIR /home/soccer
# create and activate virtual environment
RUN python3 -m venv venv
# copy and install pip requirements
COPY --chown=soccer ./requirements /home/soccer/requirements/
RUN pip3 install -r /home/soccer/requirements/${PIP_REQUIREMENTS}
# copy Django project files
COPY --chown=soccer . /home/soccer/
docker-compose.yml:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static_volume:/home/soccer/static
- media_volume:/home/soccer/media
depends_on:
- gunicorn
gunicorn:
build:
context: .
args:
PIP_REQUIREMENTS: "${PIP_REQUIREMENTS}"
command: bash -c "/home/soccer/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 soccer.wsgi:application"
depends_on:
- db
volumes:
- static_volume:/home/soccer/static
- media_volume:/home/soccer/media
expose:
- "8000"
environment:
DJANGO_SETTINGS_MODULE: "${DJANGO_SETTINGS_MODULE}"
DJANGO_SECRET_KEY: "${DJANGO_SECRET_KEY}"
DATABASE_NAME: "${DATABASE_NAME}"
DATABASE_USER: "${DATABASE_USER}"
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
db:
image: postgres:latest
restart: always
environment:
POSTGRES_DB: "${DATABASE_NAME}"
POSTGRES_USER: "${DATABASE_USER}"
POSTGRES_PASSWORD: "${DATABASE_PASSWORD}"
ports:
- 5432
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_data:
static_volume:
media_volume:
我发现了问题。我正在覆盖 venv。解决方案是将虚拟环境添加到 .dockerinfo 文件。
我正在尝试使用 gunicorn 和 nginx 将 django docker化为 docker 图像。来自 this tutorial
运行 docker-compose up --detach --build
一切构建成功。但是 gunicorn 没有启动。错误日志显示:
bash: /home/soccer/venv/bin/gunicorn: /Users/Umar/PycharmProjects/soccer/venv/bin/python: bad interpreter: No such file or directory
我该如何解决这个问题?
Dockerfile:
# pull official base image
FROM python:3.7
# accept arguments
ARG PIP_REQUIREMENTS=production.txt
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install --upgrade pip setuptools
# create user for the Django project
RUN useradd -ms /bin/bash soccer
# set current user
USER soccer
# set work directory
WORKDIR /home/soccer
# create and activate virtual environment
RUN python3 -m venv venv
# copy and install pip requirements
COPY --chown=soccer ./requirements /home/soccer/requirements/
RUN pip3 install -r /home/soccer/requirements/${PIP_REQUIREMENTS}
# copy Django project files
COPY --chown=soccer . /home/soccer/
docker-compose.yml:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static_volume:/home/soccer/static
- media_volume:/home/soccer/media
depends_on:
- gunicorn
gunicorn:
build:
context: .
args:
PIP_REQUIREMENTS: "${PIP_REQUIREMENTS}"
command: bash -c "/home/soccer/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 soccer.wsgi:application"
depends_on:
- db
volumes:
- static_volume:/home/soccer/static
- media_volume:/home/soccer/media
expose:
- "8000"
environment:
DJANGO_SETTINGS_MODULE: "${DJANGO_SETTINGS_MODULE}"
DJANGO_SECRET_KEY: "${DJANGO_SECRET_KEY}"
DATABASE_NAME: "${DATABASE_NAME}"
DATABASE_USER: "${DATABASE_USER}"
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
db:
image: postgres:latest
restart: always
environment:
POSTGRES_DB: "${DATABASE_NAME}"
POSTGRES_USER: "${DATABASE_USER}"
POSTGRES_PASSWORD: "${DATABASE_PASSWORD}"
ports:
- 5432
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_data:
static_volume:
media_volume:
我发现了问题。我正在覆盖 venv。解决方案是将虚拟环境添加到 .dockerinfo 文件。