简体   繁体   English

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

[英]Is there a way to configure docker compose to share python packages between container services?

I'm trying to dockerize my django app with a couple of celery workers. 我正在尝试与几个芹菜工人一起对我的django应用程序进行docker化。 The django app and the celery workers all create the same container - same python image, same pipenv, same packages installed from pipenv, same app. django应用程序和celery工人都创建相同的容器-相同的python映像,相同的pipenv,从pipenv安装的相同软件包,相同的应用程序。 The only difference is that I want one container to run the django app server, and the other container to run my celery workers. 唯一的区别是,我希望一个容器运行django应用服务器,另一个容器运行我的芹菜工人。

When I run docker-compose up , docker is copying the app and installing the same python packages once per container. 当我运行docker-compose up ,docker正在复制应用程序并为每个容器安装相同的python软件包一次。 This takes a long time since it's doing the exact same thing 2 times. 这花费了很长时间,因为它做了2次完全相同的事情。

I want to know if there is a way I can clone my app and install the packages ONCE, and use this for all 2 containers that would otherwise install the same thing 2 times. 我想知道是否有一种方法可以克隆我的应用程序并一次安装软件包,然后将其用于所有2个容器,否则将安装2次相同的东西。

Dockerfile 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 泊坞窗,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"

What I would do is define in your compose an image, and the other application uses that image: 我会做的是在你的定义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"

This way you are only build ing once, and the other app just uses that built image 这样,您只需build一次,而其他应用仅使用该构建的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM