Skip to content

Commit

Permalink
fix: Move the custom php8.0-xdebug build from ddev-webserver to user …
Browse files Browse the repository at this point in the history
…Dockerfile (ddev#6718)

Co-authored-by: Stanislav Zhuk <[email protected]>
  • Loading branch information
rfay and stasadev authored Nov 15, 2024
1 parent 7399693 commit fae8423
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 41 deletions.
55 changes: 20 additions & 35 deletions containers/ddev-php-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,25 @@ RUN apt-get -qq install --no-install-recommends --no-install-suggests -y \
wget
RUN url="https://github.com/mikefarah/yq/releases/latest/download/yq_linux_${TARGETPLATFORM#linux/}"; wget ${url} -q -O /usr/bin/yq && chmod +x /usr/bin/yq
ADD generic-files /

RUN curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
RUN echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

RUN curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb && \
dpkg -i /tmp/debsuryorg-archive-keyring.deb && rm -f /tmp/debsuryorg-archive-keyring.deb && \
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list && apt-get -qq update
#END base

### ---------------------------ddev-xdebug-build--------------------------------------
### ---------------------------ddev-php-extension-build--------------------------------------
FROM base AS ddev-php-extension-build
SHELL ["/bin/bash", "-c"]
### Xdebug compile specific version because 3.3 is not fully reliable
### See https://github.com/ddev/ddev/issues/6159
### We don't need to recompile every Xdebug library https://xdebug.org/docs/compat (only PHP 8.0, 8.1, 8.2 can have Xdebug 3.2)
### PECL does not allow you to install multiple versions of Xdebug, so there is `rm -f xdebug.reg`
FROM base AS ddev-xdebug-build
SHELL ["/bin/bash", "-c"]
RUN curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb && \
dpkg -i /tmp/debsuryorg-archive-keyring.deb && rm -f /tmp/debsuryorg-archive-keyring.deb && \
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list && apt-get update
ARG XDEBUG_VERSION="3.2.2"
ARG XDEBUG_BUILD_PACKAGES="build-essential php-pear php8.0-dev php8.1-dev php8.2-dev"
RUN set -eu -o pipefail; \
apt-get -qq update && \
apt-get -qq install --no-install-recommends --no-install-suggests -y ${XDEBUG_BUILD_PACKAGES}
RUN pecl channel-update pecl.php.net && \
for version in 8.0 8.1 8.2; do \
(apt-get -qq remove -y php${version}-xdebug || true) && \
pecl -d php_suffix=${version} install -f xdebug-${XDEBUG_VERSION} && \
rm -f /usr/share/php/.registry/.channel.pecl.php.net/xdebug.reg || exit $?; \
done
#END ddev-xdebug-build
RUN /usr/local/bin/build_php_extension.sh "php8.1" "xdebug" "3.2.2" "/usr/lib/php/20210902/xdebug.so"
RUN /usr/local/bin/build_php_extension.sh "php8.2" "xdebug" "3.2.2" "/usr/lib/php/20220829/xdebug.so"
#END ddev-php-extension-build

### ---------------------------ddev-php-base--------------------------------------
### Build ddev-php-base, which is the base for ddev-php-prod and ddev-webserver-*
Expand All @@ -77,15 +72,6 @@ ARG BUILDPLATFORM

SHELL ["/bin/bash", "-c"]

RUN curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
RUN echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

RUN curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb && \
dpkg -i /tmp/debsuryorg-archive-keyring.deb && rm -f /tmp/debsuryorg-archive-keyring.deb && \
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list && apt-get update

RUN apt-get -qq update
RUN apt-get -qq install --no-install-recommends --no-install-suggests -y \
apache2 \
Expand Down Expand Up @@ -121,13 +107,12 @@ RUN mkdir -p /etc/nginx/sites-enabled /var/lock/apache2 /var/log/apache2 /var/ru
chmod ugo+rx /usr/local/bin/* && \
ln -sf /usr/sbin/php-fpm${PHP_DEFAULT_VERSION} /usr/sbin/php-fpm

### ---------------------------ddev-xdebug-build--------------------------------------
### ---------------------------ddev-php-extension-build--------------------------------------
### The dates from /usr/lib/php/YYYYMMDD/ represent PHP API versions https://unix.stackexchange.com/a/591771
RUN apt-get -qq remove -y php8.0-xdebug php8.1-xdebug php8.2-xdebug
COPY --from=ddev-xdebug-build /usr/lib/php/20200930/xdebug.so /usr/lib/php/20200930/xdebug.so
COPY --from=ddev-xdebug-build /usr/lib/php/20210902/xdebug.so /usr/lib/php/20210902/xdebug.so
COPY --from=ddev-xdebug-build /usr/lib/php/20220829/xdebug.so /usr/lib/php/20220829/xdebug.so
#END ddev-xdebug-build
RUN apt-get -qq remove -y php8.1-xdebug php8.2-xdebug
COPY --from=ddev-php-extension-build /usr/lib/php/20210902/xdebug.so /usr/lib/php/20210902/xdebug.so
COPY --from=ddev-php-extension-build /usr/lib/php/20220829/xdebug.so /usr/lib/php/20220829/xdebug.so
#END ddev-php-extension-build
RUN phpdismod xhprof
RUN apt-get -qq autoremove -y
RUN curl -L --fail -o /usr/local/bin/composer -sSL https://getcomposer.org/composer-stable.phar && chmod ugo+wx /usr/local/bin/composer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

set -eu -o pipefail

if [ "$#" -ne 4 ]; then
echo "Usage: $0 <PHP_VERSION> <EXTENSION_NAME> <EXTENSION_VERSION> <EXTENSION_FILE>"
exit 1
fi

# 8.0
PHP_VERSION=${1/php/}
# xdebug
EXTENSION_NAME=$2
# 3.2.2, latest
EXTENSION_VERSION=$3
# /usr/lib/php/20210902/xdebug.so
# The dates from /usr/lib/php/YYYYMMDD/ represent PHP API versions https://unix.stackexchange.com/a/591771
EXTENSION_FILE=$4

# install pecl
if ! command -v pecl >/dev/null 2>&1 || [ "$(dpkg -l | grep "php${PHP_VERSION}-dev")" = "" ]; then
echo "Installing pecl to build php${PHP_VERSION}-${EXTENSION_NAME}"
apt-get -qq update -o Dir::Etc::sourcelist="sources.list.d/php.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" || true
apt-get -qq update -o Dir::Etc::sourcelist="sources.list.d/debian.sources" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" || true
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends --no-install-suggests -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y build-essential php-pear "php${PHP_VERSION}-dev" || exit $?
fi

if [ -f "${EXTENSION_FILE}" ]; then
echo "Moving existing ${EXTENSION_FILE} to ${EXTENSION_FILE}.bak"
mv "${EXTENSION_FILE}" "${EXTENSION_FILE}.bak"
fi

if [ "${EXTENSION_VERSION}" = "latest" ]; then
PECL_EXTENSION="${EXTENSION_NAME}"
else
PECL_EXTENSION="${EXTENSION_NAME}-${EXTENSION_VERSION}"
fi

# PECL does not allow to install multiple versions of extension at the same time,
# use `rm -f /usr/share/php/.registry/.channel.pecl.php.net/extension.reg` to make it forget about another version.
(pecl channel-update pecl.php.net && \
echo "Building php${PHP_VERSION}-${EXTENSION_NAME}..." && \
pecl -d php_suffix="${PHP_VERSION}" install -f "${PECL_EXTENSION}" >/dev/null && \
rm -f "/usr/share/php/.registry/.channel.pecl.php.net/${EXTENSION_NAME}.reg") || true

if [ ! -f "${EXTENSION_FILE}" ]; then
echo "Failed to build ${EXTENSION_FILE}"

if [ -f "${EXTENSION_FILE}.bak" ]; then
echo "Restoring previously existing file ${EXTENSION_FILE}"
mv "${EXTENSION_FILE}.bak" "${EXTENSION_FILE}"
fi

exit 2
fi

echo "Done building php${PHP_VERSION}-${EXTENSION_NAME} to ${EXTENSION_FILE}"
exit 0
2 changes: 1 addition & 1 deletion containers/ddev-webserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Build ddev-php-base from ddev-webserver-base
### ddev-php-base is the basic of ddev-php-prod
### and ddev-webserver-* (For DDEV local Usage)
FROM ddev/ddev-php-base:20241111_stasadev_n_install_cleanup AS ddev-webserver-base
FROM ddev/ddev-php-base:20241112_remove_php8.0-xdebug AS ddev-webserver-base
SHELL ["/bin/bash", "-eu","-o", "pipefail", "-c"]
ENV DEBIAN_FRONTEND=noninteractive

Expand Down
46 changes: 43 additions & 3 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,10 +1114,41 @@ func WriteBuildDockerfile(app *DdevApp, fullpath string, userDockerfilePath stri
return err
}

// Normal starting content is the arg and base image
contents := `
#ddev-generated - Do not modify this file; your modifications will be overwritten.
contents := "#ddev-generated - Do not modify this file; your modifications will be overwritten.\n"

// Build custom PHP extensions if needed
// See /usr/local/bin/build_php_extension.sh
phpExtensions := []map[string]string{
// compile specific Xdebug version https://github.com/ddev/ddev/issues/6159
{
"php": nodeps.PHP80,
"name": "xdebug",
"version": "3.2.2",
"file": "/usr/lib/php/20200930/xdebug.so",
},
}
if strings.Contains(fullpath, "webimageBuild") {
hasMultiStageBuild := false
for _, ext := range phpExtensions {
if app.PHPVersion == ext["php"] {
if !hasMultiStageBuild {
contents = contents + `
### DDEV-injected custom build for PHP extensions
ARG BASE_IMAGE="scratch"
FROM $BASE_IMAGE AS ddev-php-extension-build
SHELL ["/bin/bash", "-c"]
`
hasMultiStageBuild = true
}
contents = contents + fmt.Sprintf(`
RUN /usr/local/bin/build_php_extension.sh "php%s" "%s" "%s" "%s" || true
`, ext["php"], ext["name"], ext["version"], ext["file"])
}
}
}

// Normal starting content is the arg and base image
contents = contents + `
### DDEV-injected base Dockerfile contents
ARG BASE_IMAGE="scratch"
FROM $BASE_IMAGE
Expand All @@ -1142,6 +1173,15 @@ RUN (groupadd --gid $gid "$username" || groupadd "$username" || true) && (userad
RUN /usr/local/bin/install_php_extensions.sh "php%s" "${TARGETARCH}"
`, app.PHPVersion)
}
for _, ext := range phpExtensions {
if app.PHPVersion == ext["php"] {
contents = contents + fmt.Sprintf(`
### DDEV-injected copy of %s %v
RUN apt-get -qq remove -y php%s-%s || true
COPY --from=ddev-php-extension-build %s %v
`, ext["name"], ext["version"], app.PHPVersion, ext["name"], ext["file"], ext["file"])
}
}
}

// If there are user pre.Dockerfile* files, insert their contents
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddevapp/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestProcessHooks(t *testing.T) {
//{"composer: install", "", "Running task: Composer command '[install]' in web container"},
//{"composer: licenses --format=json", "no-version-set", "Running task: Composer command 'licenses --format=json' in web container"},
//{"composer:\n exec_raw: [licenses, --format=json]", "no-version-set", "Running task: Composer command '[licenses --format=json]' in web container"},
{"exec: ls /usr/local/bin", "acli\ncomposer", "Running task: Exec command 'ls /usr/local/bin'"},
{"exec: ls /usr/local/bin", "acli\nbuild_php_extension.sh\ncomposer", "Running task: Exec command 'ls /usr/local/bin'"},
{"exec-host: \"echo something\"", "something\n", "Running task: Exec command 'echo something' on the host"},
{"exec: echo MYSQL_PWD=${MYSQL_PWD:-}\n service: db", "MYSQL_PWD=db\n", "Running task: Exec command 'echo MYSQL_PWD=${MYSQL_PWD:-}' in container/service 'db'"},
{"exec: \"echo TestProcessHooks > /var/www/html/TestProcessHooks-php-version-${DDEV_PHP_VERSION}.txt\"", "", "Running task: Exec command 'echo TestProcessHooks > /var/www/html/TestProcessHooks-php-version-${DDEV_PHP_VERSION}.txt'"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/versionconstants/versionconstants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var AmplitudeAPIKey = ""
var WebImg = "ddev/ddev-webserver"

// WebTag defines the default web image tag
var WebTag = "20241111_stasadev_n_install_cleanup" // Note that this can be overridden by make
var WebTag = "20241112_remove_php8.0-xdebug" // Note that this can be overridden by make

// DBImg defines the default db image used for applications.
var DBImg = "ddev/ddev-dbserver"
Expand Down

0 comments on commit fae8423

Please sign in to comment.