How to avoid reinstalling packages when building Docker image for Python projects?

前端 未结 4 574
天涯浪人
天涯浪人 2020-12-04 07:24

My Dockerfile is something like

FROM my/base

ADD . /srv
RUN pip install -r requirements.txt
RUN python setup.py install

ENTRYPOINT ["run_server"]
         


        
4条回答
  •  生来不讨喜
    2020-12-04 07:53

    I found that a better way is to just add the Python site-packages directory as a volume.

    services:
        web:
            build: .
            command: python manage.py runserver 0.0.0.0:8000
            volumes:
                - .:/code
                -  /usr/local/lib/python2.7/site-packages/
    

    This way I can just pip install new libraries without having to do a full rebuild.

    EDIT: Disregard this answer, jkukul's answer above worked for me. My intent was to cache the site-packages folder. That would have looked something more like:

    volumes:
       - .:/code
       - ./cached-packages:/usr/local/lib/python2.7/site-packages/
    

    Caching the download folder is alot cleaner though. That also caches the wheels, so it properly achieves the task.

提交回复
热议问题