首页 > 解决方案 > 使用 docker-compose 'command:' 运行时,Supervisord 退出 Docker 容器,但从 bash 运行时不会退出

问题描述

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

如果我将容器命令设置为sleep 3600(这样我可以在它崩溃之前 bash 进入),Supervisord 就会运行,然后 bash 进入容器并运行supervisord -c supervisord.conf. 但是,如果我将容器上的命令设置为相同的命令,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

标签: pythondockerdocker-composesupervisorddjango-rq

解决方案


对于遇到此问题的任何人 - 解决方案非常简单。您必须在前台运行 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

推荐阅读