有没有一种方法可以配置docker compose在容器服务之间共享python包?

时间:2019-04-08 16:29:40

标签: python docker docker-compose

我正试图与几个芹菜工人对我的django应用程序进行docker化。 django应用程序和celery工人都创建相同的容器-相同的python映像,相同的pipenv,从pipenv安装的相同软件包,相同的应用程序。唯一的区别是,我希望一个容器运行django应用服务器,另一个容器运行我的芹菜工人。

当我运行docker-compose up时,泊坞窗正在复制应用程序并为每个容器安装相同的python软件包一次。这花了很长时间,因为它做了2次完全相同的事情。

我想知道是否有一种方法可以克隆我的应用程序并一次安装软件包,并将其用于所有2个容器,否则将安装2次相同的东西。

Dockerfile

FROM python:3.5.6
COPY . /app/
WORKDIR /app/
RUN pip install pipenv==2018.11.26
ADD Pipfile Pipfile
RUN pipenv install --deploy --system
EXPOSE 8000

Docker-compose.yml

version: '2'
services:
  app:
    restart: always
    build: .
    expose:
      - "8000"
    container_name: "app"
    image: debian/latest
    links:
      - postgres
      - redis
    depends_on:
      - postgres
      - redis
    ports:
      - '8000:8000'
    networks:
      - network1
      - nginx_network
    volumes:
      - ./:/app
      - ./data:/app/data
      - static_volume:/app/static
      - ./logs:/app/logs
    entrypoint: ["sh", "/app/docker-entrypoint.sh"]
    env_file:
      - .env
    environment:
      - DJANGO_SETTINGS_MODULE=app.settings.production
  celery_default:
    restart: always
    build: .
    container_name: "celery_default"
    networks:
      - network1
    links:
      - redis
      - postgres
    depends_on:
      - postgres
      - redis
      - celerybeat
    volumes:
      - ./:/app
      - ./data:/app/data
      - ./logs:/app/logs
      - ./celery:/app/celery
    env_file:
      - .env
    entrypoint: "celery -A app worker -Q celery -l debug -n celery_worker --concurrency=2 --logfile=./celery/logs/default.log"

1 个答案:

答案 0 :(得分:1)

我要做的是在您的compose中定义一个图像,然后其他应用程序使用该图像:

version: '2'
services:
  app:
    restart: always
    build: .
    image: your-custom-image # Notice I've created a custom image tag here
    expose:
      - "8000"
    container_name: "app"
    links:
      - postgres
      - redis
    depends_on:
      - postgres
      - redis
    ports:
      - '8000:8000'
    networks:
      - network1
      - nginx_network
    volumes:
      - ./:/app
      - ./data:/app/data
      - static_volume:/app/static
      - ./logs:/app/logs
    entrypoint: ["sh", "/app/docker-entrypoint.sh"]
    env_file:
      - .env
    environment:
      - DJANGO_SETTINGS_MODULE=app.settings.production
  celery_default:
    restart: always
    image: your-custom-image # No build directory, just reuse that image
    container_name: "celery_default"
    networks:
      - network1
    links:
      - redis
      - postgres
    depends_on:
      - postgres
      - redis
      - celerybeat
    volumes:
      - ./:/app
      - ./data:/app/data
      - ./logs:/app/logs
      - ./celery:/app/celery
    env_file:
      - .env
    entrypoint: "celery -A app worker -Q celery -l debug -n celery_worker --concurrency=2 --logfile=./celery/logs/default.log"

这样,您只需build一次,而其他应用程序仅使用该内置图像