15 ms·
The reason is to minimize the number of layers, mostly the layers that are not used in the final image (in this example, a layer would include gcc while another
by vbernat 6y ago
The reason is to minimize the number of layers, mostly the layers that are not used in the final image (in this example, a layer would include gcc while another would remove it, but you would still need to download the layer with gcc).
- claytonjy 6y agoI thought non-final stages in a multi-stage build are left out of the final artifact; is that incorrect? I'm thinking the "build step" would be done in an earlier stage; it could be the exact same RUN statement, or it could be split into multiple for readability, and wouldn't bother removing any installed packages, since they won't carry over to the final stage. Then the big RUN in the final stage would be replaced with something like `COPY --from=builder ...`.
- verst 6y agoEDIT: Provide example below If you are doing multi stage builds it only matters to combined as many statements as possible in the last layer. I agree that for clarity it is nice not to optimize layers in the build stage - those will be thrown away anyway. I vastly prefer multi stage builds over having to chain install and cleanup statements Example: I usually want to use the python:3-slim image, but this doesn't have the tools to compile certain python libraries with C extensions. Generally I will use the python:3 image for my build stage to do my "pip install -r requirements.txt" and then copy the libraries over to my final stage based on the python:3-slim image Of course I could install and uninstall GCC and other tools in a single stage.. but that actually takes longer to do and is messier in my opinion.
- jackthetab 6y ago> Example: I usually want to use the python:3-slim image, but this doesn't have the tools to compile certain python libraries with C extensions. Generally I will use the python:3 image for my build stage to do my "pip install -r requirements.txt" and then copy the libraries over to my final stage based on the python:3-slim image Example on how to do that, please.