我目前正在使用 docker-compose 来使用 python 队列管理器 Celery 的一个实例。但是,当我扩展我的代码时,我希望能够使用 Google 的 OR-tools 包。但是,如果您使用的是 python-3.6:alpine(如此处讨论:pip install ortools: No matching distribution - Alpine),则必须从源代码安装 OR-tools。我曾尝试使用非 Alpine 版本的 python,但后来我遇到了 Celery 的问题,所以我认为从源代码安装是前进的方向。
我对使用 Docker 比较陌生,所以我很难做到这一点。只需添加 RUN git clone -b master https://github.com/google/or-tools
和此处提到的其余命令:https://developers.google.com/optimization/install/python/source_linux到 Dockerfile 不起作用。它说 git 不是命令,但如果我尝试使用 RUN sudo apt install git
安装它,它会告诉我 sudo 不是命令。
我是否需要向我的 dockerfile 添加另一个部分,例如:
FROM: ubuntu
RUN git clone -b master https://github.com/google/or-tools
如果是这样,执行此操作的正确方法是什么,并确保它链接到我的 Dockerfile 的其他部分,以便我可以在 python 代码中使用 OR-tools?
docker 文件:
FROM python:3.6-alpine
ENV CELERY_BROKER_URL redis://redis:6379/0
ENV CELERY_RESULT_BACKEND redis://redis:6379/0
ENV C_FORCE_ROOT true
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD celery -A celeryExample worker --concurrency=1 --loglevel=info
DOCKER-COMPOSE:
version: '3'
services:
redis:
image: redis
ports:
- 6379:6379
celery:
build:
context: .
dockerfile: Dockerfile.celery
depends_on:
- redis
volumes:
- .:/usr/src/app
要求:
celery
redis
requests
最佳答案
你已经很接近了,在同一次执行中从其他图像中获取依赖项就可以了。
尝试以下 Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y git
RUN git clone -b master https://github.com/google/or-tools
FROM python:3.6-alpine
ENV CELERY_BROKER_URL redis://redis:6379/0
ENV CELERY_RESULT_BACKEND redis://redis:6379/0
ENV C_FORCE_ROOT true
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD celery -A celeryExample worker --concurrency=1 --loglevel=info
关于python - 在 Dockerfile 中从源代码安装 python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645260/