Skip to content

Instantly share code, notes, and snippets.

@rwese
Created November 2, 2022 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwese/1d786e88edc2e4e81175e34dd8cbea8e to your computer and use it in GitHub Desktop.
Save rwese/1d786e88edc2e4e81175e34dd8cbea8e to your computer and use it in GitHub Desktop.
PHP Docker Container wrapper compatible with phpstorm
#!/usr/bin/env bash
#
# @author René Weselowski
#
# php cli wrapper for the php-docker images.
#
# @see https://hub.docker.com/_/php
#
##### requirements
# - docker
# - podman *TBD*
#
##### Install
# - copy to /usr/local/bin, or any prefered user bin directory
# - set executable
# - run it
#
##### Why
#
# run php in a docker container with local path mappings to debug different
# verions without installing them locally.
#
##### Supported versions
#
# - php < 7.4
#
# Attention to xdebug
#
# - with php 7.4 the xdebug version and thus configuration slightly changes
#
##### Setup for PHPStorm
#
# do not use pathmappings since we bind-mount the /home
# this allows to run&debug from-scratch php scripts without troubles.
#
##### Todo
#
# - add xdebug3 toggle
# - add podman/rootless support
# - add proper entrypoint script to setup php.ini and run php properly
#
##### usage: php <args...>
#
# build)
# build is given the docker container and image will be rebuild
# exits with code 255
#
# all other and args commands will be run directly against the php binary.
#
# $ php -v
#
set -eu
PHP_VERSION="${PHP_VERSION:-7.1-cli}"
XDEBUG_VERSION="${XDEBUG_VERSION:-2}"
PECL_PACKAGE_XDEBUG="${PECL_PACKAGE_XDEBUG:-xdebug-2.8.1}"
IMAGENAME="php-docker-${PHP_VERSION}"
# SERVER_NAME configured in phpStorm, can be same for all versions, only used for pathmappings
SERVERNAME="php-docker"
# REMOTE_HOST is the network-ip of the docker bridge, your machine
REMOTE_HOST=172.17.0.1
NEW_BUILD=0
BUILD_NO_CACHE=
IMAGE_EXISTS=$(test "$(docker image ls -q "${IMAGENAME}")"; echo $?)
if [[ $IMAGE_EXISTS -eq 1 ]]; then
echo "no image has been built, forcing build."
NEW_BUILD=1
fi
if [[ "$1" == "build" ]]; then
(docker rm -f "${IMAGENAME}" 2>&1 | grep -v 'Error: No such container') || true
docker image rm "${IMAGENAME}" || echo 'no image to remove'
NEW_BUILD=1
BUILD_NO_CACHE=--no-cache
fi
DOCKER_LIB_XDEBUG_pre_74=$(cat << EOF
RUN pecl install ${PECL_PACKAGE_XDEBUG} \
&& docker-php-ext-enable xdebug
EOF
)
# DOCKER_LIB_XDEBUG_74=$(cat << EOF
# EOF
# )
DOCKER_LIB_ZIP_pre_74=$(cat << EOF
RUN apt-get install -y \
unzip \
libzip-dev \
zip \
&& docker-php-ext-configure zip --with-libzip \
&& docker-php-ext-install zip
EOF
)
# DOCKER_LIB_ZIP_74=$(cat << EOF
# RUN apt-get install -y \
# unzip \
# libzip-dev \
# zip \
# && docker-php-ext-install zip
# EOF
# )
DOCKER_LIB_ZIP=${DOCKER_LIB_ZIP_pre_74}
DOCKER_LIB_XDEBUG=${DOCKER_LIB_XDEBUG_pre_74}
if [[ $NEW_BUILD -eq 1 ]]; then
DOCKER_OUTPUT=$(docker build ${BUILD_NO_CACHE} -t "${IMAGENAME}" - 2>&1 <<- DOCKERFILE
FROM php:${PHP_VERSION}
ENV PHP_INI_DIR /usr/local/etc/php
RUN apt-get update
${DOCKER_LIB_ZIP}
${DOCKER_LIB_XDEBUG}
RUN cp "\$PHP_INI_DIR/php.ini-development" "\$PHP_INI_DIR/php.ini"
RUN ln -s "/home/rweselowski" "\$PHP_INI_DIR/conf.d/php_user.ini"
ENTRYPOINT ["sh"]
DOCKERFILE
)
echo "$DOCKER_OUTPUT"
exit 255
fi
# docker run if container is not running
IS_RUNNING="$(docker ps -q -f name="${IMAGENAME}")"
if [ -z "$IS_RUNNING" ]; then
docker run -d -it --name "${IMAGENAME}" --rm \
-v /home:/home:ro \
-v /tmp:/tmp:rw \
--env PHP_IDE_CONFIG="serverName=${SERVERNAME}" \
--env XDEBUG_CONFIG="remote_host=${REMOTE_HOST}" \
-w /home "${IMAGENAME}" &>/dev/null
fi
docker exec -w "$(pwd)" "${IMAGENAME}" \
php -dxdebug.remote_host=${REMOTE_HOST} \
-derror_reporting=E_ALL \
-ddisplay_errors=1 \
-ddisplay_startup_errors=1 \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment