当使用 docker-compose 'command:' 运行时,Supervisord 在 Docker 容器内退出,但在从 bash 运行时不会退出

问题描述

我正在尝试在 docker 容器中运行 DjangoRQ 工作人员 - 一个简单的“工作人员”容器,我将在数字海洋水滴上运行。我正在使用 supervisord 来运行多个工作人员。

如果我将容器命令设置为 sleep 3600(这样我可以在它崩溃之前冲入)然后冲入容器并运行 supervisord -c supervisord.conf,Supervisord 将运行。但是,如果我将容器上的命令设置为完全相同的命令,command: supervisord -c supervisord.conf 然后容器退出Unlinking stale socket /tmp/supervisor.sock

 worker:
    build:
      context: ./
      dockerfile: DockerfileWorker
    env_file:
      - .env
    environment:
      - DJANGO_CONfig
    volumes:
      - .:/dask 
    depends_on:
      - postgres
      - redis
    command: sleep 3600
# `python-base` sets up all our shared environment variables
FROM ubuntu:latest

    # python
ENV PYTHONUNBUFFERED=1 \
    # prevents python creating .pyc files
    PYTHONDONTWRITEBYTECODE=1 \
    \
    # pip
    PIP_NO_CACHE_DIR=off \
    PIP_disABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    \
    # poetry
    # https://python-poetry.org/docs/configuration/#using-environment-variables
    POETRY_VERSION=1.1.5 \
    # make poetry install to this location
    POETRY_HOME="/opt/poetry" \
    # make poetry create the virtual environment in the project's root
    # it gets named `.venv`
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    # do not ask any interactive question
    POETRY_NO_INteraCTION=1 \
    \
    # paths
    # this is where our requirements + virtual environment will live
    PYSETUP_PATH="/opt/pysetup" \
    VENV_PATH="/opt/pysetup/.venv" \
    LOG_LEVEL=DEBUG


# prepend poetry and venv to path
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

RUN apt-get update \
    && apt-get install --no-install-recommends -y \
        # deps for installing poetry
        curl \
        # deps for building python deps
        build-essential \
        python3-pip

# install poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN pip3 install poetry

# copy project requirement files here to ensure they will be cached.
workdir $PYSETUP_PATH
copY poetry.lock pyproject.toml ./

# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --no-dev

# # `production` image used for runtime
# FROM python-base as production
copY . /dask
copY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
workdir /dask

解决方法

对于遇到此问题的任何人 - 解决方案非常简单。您必须在前台运行 supervisord 否则 Docker 不认为进程正在运行,因此它退出。将 -n 标签添加到 supervisord 中,你会很高兴。

worker:
    build:
      context: ./
      dockerfile: DockerfileWorker
    env_file:
      - .env
    environment:
      - DJANGO_CONFIG
    volumes:
      - .:/dask 
    depends_on:
      - postgres
      - redis
     command: supervisord -n -c supervisord.conf