How do I reduce a python (docker) image size using a multi-stage build?

后端 未结 3 1179
心在旅途
心在旅途 2020-12-31 03:05

I am looking for a way to create multistage builds with python and Dockerfile:

For example, using the following images:

1st image: install

3条回答
  •  离开以前
    2020-12-31 03:31

    I recommend the approach detailed in this article (section 2). He uses virtualenv so pip install stores all the python code, binaries, etc. under one folder instead of spread out all over the file system. Then it's easy to copy just that one folder to the final "production" image. In summary:

    Compile image

    • Activate virtualenv in some path of your choosing.
    • Prepend that path to your docker ENV. This is all virtualenv needs to function for all future docker RUN and CMD action.
    • Install system dev packages and pip install xyz as usual.

    Production image

    • Copy the virtualenv folder from the Compile Image.
    • Prepend the virtualenv folder to docker's PATH

提交回复
热议问题