From 60adf74602d4611126b102d28f2c822872fc4065 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Fri, 8 Sep 2023 09:16:08 -0400 Subject: [PATCH] docker: Update logic for installing nodejs (#32926) Nodesource has changed their installation instructions. Update our Dockerfile to use the new method. Also, remove a few more unneeded log files and some other optimizations. --- tools/cli/commands/docker.js | 33 ++++++++++++++++++------------- tools/docker/Dockerfile | 38 +++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/tools/cli/commands/docker.js b/tools/cli/commands/docker.js index f439cf7c3ae9c..0c6a4f727d7f4 100644 --- a/tools/cli/commands/docker.js +++ b/tools/cli/commands/docker.js @@ -568,20 +568,25 @@ export function dockerDefine( yargs ) { fs.readFileSync( `${ dockerFolder }/../../.github/versions.sh`, 'utf8' ) ); const res = executor( argv, () => - shellExecutor( argv, 'docker', [ - 'build', - '-t', - 'automattic/jetpack-wordpress-dev', - '--build-arg', - `PHP_VERSION=${ versions.PHP_VERSION }`, - '--build-arg', - `COMPOSER_VERSION=${ versions.COMPOSER_VERSION }`, - '--build-arg', - `NODE_VERSION=${ versions.NODE_VERSION }`, - '--build-arg', - `PNPM_VERSION=${ versions.PNPM_VERSION }`, - dockerFolder, - ] ) + shellExecutor( + argv, + 'docker', + [ + 'build', + '-t', + 'automattic/jetpack-wordpress-dev', + '--build-arg', + `PHP_VERSION=${ versions.PHP_VERSION }`, + '--build-arg', + `COMPOSER_VERSION=${ versions.COMPOSER_VERSION }`, + '--build-arg', + `NODE_VERSION=${ versions.NODE_VERSION }`, + '--build-arg', + `PNPM_VERSION=${ versions.PNPM_VERSION }`, + dockerFolder, + ], + { env: { DOCKER_BUILDKIT: 1 } } + ) ); checkProcessResult( res ); }, diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile index 0631af2b5435d..86fa17e1f13d9 100644 --- a/tools/docker/Dockerfile +++ b/tools/docker/Dockerfile @@ -13,15 +13,15 @@ ENV LC_ALL en_US.UTF-8 WORKDIR /tmp # Install basic packages, including Apache. -RUN \ +RUN --mount=type=cache,target=/var/lib/apt/lists/,sharing=private \ export DEBIAN_FRONTEND=noninteractive \ && apt-get update \ - && apt-get install -y language-pack-en-base software-properties-common \ + && apt-get install -y curl gpg language-pack-en-base software-properties-common \ && add-apt-repository ppa:ondrej/php \ + && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && apt-get update \ && apt-get install -y \ apache2 \ - curl \ git \ jq \ less \ @@ -34,17 +34,16 @@ RUN \ unzip \ vim \ zip \ - && apt-get remove --purge --auto-remove -y software-properties-common \ - && rm -rf /var/lib/apt/lists/* ~/.launchpadlib + && apt-get remove --purge --auto-remove -y gpg software-properties-common \ + && find /var/ -name '*-old' -delete && rm -rf /var/log/dpkg.log /var/log/alternatives.log /var/log/apt/ ~/.launchpadlib # Enable mod_rewrite in Apache. RUN a2enmod rewrite # Install requested version of PHP. -RUN \ +RUN --mount=type=cache,target=/var/lib/apt/lists/,sharing=private \ : "${PHP_VERSION:?Build argument PHP_VERSION needs to be set and non-empty.}" \ && export DEBIAN_FRONTEND=noninteractive \ - && apt-get update \ && apt-get install -y \ libapache2-mod-php${PHP_VERSION} \ php${PHP_VERSION} \ @@ -67,29 +66,32 @@ RUN \ php${PHP_VERSION}-apcu \ php${PHP_VERSION}-gd \ php${PHP_VERSION}-imagick \ - && rm -rf /var/lib/apt/lists/* + && find /var/ -name '*-old' -delete && rm -rf /var/log/dpkg.log /var/log/alternatives.log /var/log/apt/ ~/.launchpadlib # Install requested version of Composer. RUN \ : "${COMPOSER_VERSION:?Build argument COMPOSER_VERSION needs to be set and non-empty.}" \ && php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ && php composer-setup.php --install-dir=/usr/local/bin --filename=composer --version=$COMPOSER_VERSION \ - && php -r "unlink('composer-setup.php');" + && php -r "unlink('composer-setup.php');" \ + && rm -rf ~/.composer # Install requested version of Node. -# We add the PPA for ease of updating, while we download the specific node version manually if possible for installation. -RUN \ +RUN --mount=type=cache,target=/var/lib/apt/lists/,sharing=private \ : "${NODE_VERSION:?Build argument NODE_VERSION needs to be set and non-empty.}" \ + && export DEBIAN_FRONTEND=noninteractive \ && N=${NODE_VERSION%%.*} \ - && curl -fSL https://deb.nodesource.com/setup_${N}.x | bash - \ - && DEB="$(curl -fSL https://deb.nodesource.com/node_${N}.x/pool/main/n/nodejs/ | perl -nwe 'BEGIN { $v = shift; $arch = shift; $re = qr/nodejs_\Q$v\E-.*_\Q$arch.deb\E/; $out=""; } $out=$1 if /href="($re)"/; END { print "$out"; }' "${NODE_VERSION}" "$(dpkg --print-architecture)")" \ - && if [ -n "$DEB" ]; then curl -fSL "https://deb.nodesource.com/node_${N}.x/pool/main/n/nodejs/$DEB" --output /tmp/nodejs.deb && dpkg -i /tmp/nodejs.deb; else DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs; fi \ - && rm -rf /var/lib/apt/lists/* /tmp/nodejs.deb + && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$N.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \ + && apt-get -q update \ + && VER="$(apt-cache show nodejs | sed -n "/^Version: ${NODE_VERSION}-/ { s/^Version: /=/p; q }" )" \ + && apt-get install -y nodejs$VER \ + && find /var/ -name '*-old' -delete && rm -rf /var/log/dpkg.log /var/log/alternatives.log /var/log/apt/ ~/.launchpadlib # Install requested version of pnpm. RUN \ : "${PNPM_VERSION:?Build argument PNPM_VERSION needs to be set and non-empty.}" \ && npm install --global pnpm@$PNPM_VERSION \ + && SHELL=/bin/bash pnpm setup \ && rm -rf ~/.npm # Install wp-cli. @@ -102,14 +104,14 @@ RUN mkdir /usr/local/src/psysh \ && composer require psy/psysh:@stable \ && mkdir ~/.wp-cli \ && echo "require: /usr/local/src/psysh/vendor/autoload.php" > ~/.wp-cli/config.yml \ - && rm -rf ~/.cache ~/.composer ~/.config ~/.local ~/.subversion + && rm -rf ~/.composer ~/.subversion # Copy a default config file for an apache host. COPY ./config/apache_default /etc/apache2/sites-available/000-default.conf # Copy a default set of settings for PHP (php.ini). -COPY ./config/php.ini /etc/php/${PHP_VERSION}/apache2/conf.d/20-jetpack-wordpress.ini -COPY ./config/php.ini /etc/php/${PHP_VERSION}/cli/conf.d/20-jetpack-wordpress.ini +COPY ./config/php.ini /etc/php/${PHP_VERSION}/mods-available/jetpack-wordpress.ini +RUN phpenmod jetpack-wordpress # Copy single site htaccess to /var/lib/jetpack-config. run.sh will move it to the site's base dir if there's none present. COPY ./config/htaccess /var/lib/jetpack-config/htaccess