docker-node
docker-node copied to clipboard
Multiple apt-get install, purge commands
I was reading the Dockerfile for node:12-slim
And I have noticed that the apt-get install and apt-get purge commands are run twice.
For first installing node:
https://github.com/nodejs/docker-node/blob/1f26041ed1cdbe7df00004006c4105e6b960fc3e/12/stretch-slim/Dockerfile#L20
https://github.com/nodejs/docker-node/blob/1f26041ed1cdbe7df00004006c4105e6b960fc3e/12/stretch-slim/Dockerfile#L53
And then for installing yarn:
https://github.com/nodejs/docker-node/blob/1f26041ed1cdbe7df00004006c4105e6b960fc3e/12/stretch-slim/Dockerfile#L63
https://github.com/nodejs/docker-node/blob/1f26041ed1cdbe7df00004006c4105e6b960fc3e/12/stretch-slim/Dockerfile#L89
Wouldn't it be efficient to run the purge commands after yarn has been installed?
This could be done by defining the YARN_VERSION environment variable before the first RUN command, and then using && to chain both the RUN commands together, where we install all needed packages before installing node, and then purge them after installing yarn.
No, because leaving them in between the commands would leave the layers larger
No, because leaving them in between the commands would leave the layers larger
Wouldn't it be just one really long command, and hence it would be only 1 layer for the RUN command.
Let me show you a rough idea of what I mean:
Line 6 onwards:
ENV NODE_VERSION 12.18.2
ENV YARN_VERSION 1.22.4
RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
.
.
.
&& apt-get update && apt-get install -y ca-certificates curl wget gnupg dirmngr xz-utils libatomic1 --no-install-recommends \
.
.
.
**(install node)**
&& find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { print $(NF-1) }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
**(test node)**
&& node --version \
&& npm --version
.
.
.
**(install yarn)**
&& find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { print $(NF-1) }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
**(test yarn)**
&& yarn --version
(finally purge all installed packages)
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false
The whole RUN command (which downloads packages, installs node, installs yarn, purges packages) should just create one single layer. There won't be any residual packages between the layers.
I think they were split originally to allow updating Yarn without rebuilding the whole image, but since Yarn 1 is in maintenance mode, I don't think that matters much anymore. You could try PR'ing something to see if the other maintainers agree. You probably need to update the *.template files and then run the update.sh script to regenerate them