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

前端 未结 4 576
天涯浪人
天涯浪人 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 08:04

    To minimise the network activity, you could point pip to a cache directory on your host machine.

    Run your docker container with your host's pip cache directory bind mounted into your container's pip cache directory. docker run command should look like this:

    docker run -v $HOME/.cache/pip-docker/:/root/.cache/pip image_1
    

    Then in your Dockerfile install your requirements as a part of ENTRYPOINT statement (or CMD statement) instead of as a RUN command. This is important, because (as pointed out in comments) the mount is not available during image building (when RUN statements are executed). Docker file should look like this:

    FROM my/base
    
    ADD . /srv
    
    ENTRYPOINT ["sh", "-c", "pip install -r requirements.txt && python setup.py install && run_server"]
    

提交回复
热议问题