docker django 的入口点行为

docker entrypoint behaviour with django

我正在尝试用 uwsgi 制作我的第一个 django 容器。它的工作原理如下:

FROM python:3.5

RUN apt-get update && \
    apt-get install -y && \
    pip3 install uwsgi

COPY ./projects.thux.it/requirements.txt /opt/app/requirements.txt
RUN pip3 install -r /opt/app/requirements.txt

COPY ./projects.thux.it /opt/app
COPY ./uwsgi.ini  /opt/app
COPY ./entrypoint /usr/local/bin/entrypoint

ENV PYTHONPATH=/opt/app:/opt/app/apps

WORKDIR /opt/app
ENTRYPOINT ["entrypoint"]
EXPOSE 8000

#CMD ["--ini", "/opt/app/uwsgi.ini"]

entrypoint 这是一个脚本,用于检测在所有其他情况下是否调用 uwsgi(如果没有参数)或 python manage。 我想将此容器用作可执行文件(dj migrate, dj shell, ... - 这里的 dj 是 python manage.py django 交互的处理程序)和一个 long -term 容器 (uwsgi --ini uwsgi.ini)。我使用 docker-compose 如下:

web:
    image: thux-projects:3.5
    build: .
    ports:
      - "8001:8000"
    volumes:
      - ./projects.thux.it/web/settings:/opt/app/web/settings
      - ./manage.py:/opt/app/manage.py
      - ./uwsgi.ini:/opt/app/uwsgi.ini
      - ./logs:/var/log/django

事实上,我设法正确地为项目提供服务,但要与 django 交互 "check" 我需要发出:

docker-compose exec web入口点检查

在阅读文档时我以为我只需要参数(没有 entrypoint

Command line arguments to docker run will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD. This allows arguments to be passed to the entry point, i.e., docker run -d will pass the -d argument to the entry point.

"repeated"入口点的工作情况:

$ docker-compose exec web entrypoint check
System check identified no issues (0 silenced).

如果我避免失败'entrypoint':

$ docker-compose exec web check
OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"check\": executable file not found in $PATH": unknown

docker exec 从不使用容器的入口点;它只是直接运行你给它的命令。

当您 docker run 一个容器时,您启动它的入口点和命令是 combined to produce a single command line, and that command becomes the main container process. On the other hand, when you docker exec 运行ning 容器中的一个命令,它按字面解释; assemble 的命令行没有两部分,根本不考虑容器的入口点。

对于您描述的用例,您不需要入口点脚本来以不寻常的方式处理命令。您可以创建一个指向 manage.py 脚本的符号链接以给 运行 它一个更短的别名,但将默认命令设为 uwsgi 运行ner.

RUN chmod +x manage.py
RUN ln -s /opt/app/manage.py /usr/local/bin/dj
CMD ["uwsgi", "--ini", "/opt/app/uwsgi.ini"]
# Runs uwsgi:
docker run -p 8000:8000 myimage

# Manually trigger database migrations:
docker run --rm myimage dj migrate