docker-node icon indicating copy to clipboard operation
docker-node copied to clipboard

Multiple apt-get install, purge commands

Open vinay-deshmukh opened this issue 5 years ago • 3 comments

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.

vinay-deshmukh avatar Jul 21 '20 03:07 vinay-deshmukh

No, because leaving them in between the commands would leave the layers larger

nschonni avatar Jul 21 '20 03:07 nschonni

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.

vinay-deshmukh avatar Jul 21 '20 04:07 vinay-deshmukh

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

nschonni avatar Jul 21 '20 04:07 nschonni