Skip to content

Instantly share code, notes, and snippets.

@gelbehexe
Last active July 17, 2023 17:12
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 gelbehexe/0e9a475e3882b3868ef28dc07cf55d05 to your computer and use it in GitHub Desktop.
Save gelbehexe/0e9a475e3882b3868ef28dc07cf55d05 to your computer and use it in GitHub Desktop.
Gist to setup a simple php cli docker machine with latest php version
.DS_Store
README.md
run.sh
tmp/**

Setup a simple php cli docker machine

Gist to setup a simple php cli docker machine with latest php version

Usage

Installation:

# cd <your project>

git clone git@gist.github.com:0e9a475e3882b3868ef28dc07cf55d05.git docker
cd docker

mkdir tmp

# optional if you do not want the files to be part of the project
echo "*" > .gitignore

# allow execution for run helper script
chmod +x ./run.sh

# optional: set your uid and guid to be used in docker environment with "gosu" (linux/mac)
# (make sure to be in a separate folder to avoid to override your project .env file)
echo -e "GOSU_UID=$(id -u)\nGOSU_GID=$(id -g)" > .env

build

# cd <your project>
docker/run.sh build

Go to docker shell

# cd <your project>
docker/run.sh shell

Go to docker root shell

# cd <your project>
docker/run.sh root-shell

Rebuild

# cd <your project>
docker/run.sh rebuild

Examples

Run unit tests

# cd <your project>
docker/run.sh php vendor/bin/phpunit

Use composer

# cd <your project>
docker/run.sh php composer

Output a list of modules

# cd <your project>
docker/run.sh php -m > docker/tmp/php_modules.txt

Check uncommitted files

# cd <your project>
git status --untracked-files --short --porcelain  | sed -r 's/^...//g' | xargs bash docker/run.sh -T phpcs -v
version: '3'
services:
php: &php
build:
context: .
dockerfile: Dockerfile
environment: &env
TZ: '${TZ:-Europe/Berlin}'
GOSU_UID: "${GOSU_UID:-1000}"
GOSU_GID: "${GOSU_GID:-1000}"
GOSU_ENABLED: "${GOSU_ENABLED:-1}"
TARGET_MODE: "php"
volumes:
- '${PWD}:/var/www/html'
working_dir: /var/www/html
shell:
<<: *php
command: ["/bin/bash"]
root-shell:
<<: *php
environment:
<<: *env
GOSU_ENABLED: "${GOSU_ENABLED:-0}"
command: ["/bin/bash"]
phpcs:
<<: *php
environment: &envphpcs
<<: *env
TARGET_MODE: "phpcs"
command: ["phpcs"]
phpcbf:
<<: *php
environment:
<<: *envphpcs
TARGET_MODE: "phpcbf"
command: ["phpcbf"]
#!/bin/sh
set -e
GOSU_UID="${GOSU_UID:-1000}"
GOSU_GID="${GOSU_GID:-1000}"
if [ ! -z "$TZ" ]; then
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
echo "date.timezone=\"$TZ\"" > /usr/local/etc/php/conf.d/99-local-timezone.ini
fi
_createDevUser() {
if [ -d "/home/dev" ]; then
useradd -Ms /bin/bash -g "${GOSU_GID}" -u "${GOSU_UID}" -d /home/dev dev
else
useradd -ms /bin/bash -g "${GOSU_GID}" -u "${GOSU_UID}" -d /home/dev dev
fi
}
createDevUser() {
groupadd --force -g "${GOSU_GID}" dev
id dev 2> /dev/null || _createDevUser
chown -R "${GOSU_GID}:${GOSU_UID}" /home/dev
}
createDevUser
if [ ! -d /.composer ]; then
mkdir /.composer
fi
chown -R "${GOSU_GID}:${GOSU_UID}" /.composer
# run scripts:
for script in /usr/local/docker/scripts.d/*.sh; do
test -e "$script" || continue
. "$script"
done
if [ "$TARGET_MODE" = "phpcs" ] || [ "$TARGET_MODE" = "phpcbf" ]; then
if [ "$TARGET_MODE" != "$1" ]; then
set -- "$TARGET_MODE" "$@"
fi
if [ "$#" -eq 1 ]; then
set -- "$@" "."
fi
# first arg is `-f` or `--some-option`
elif [ "${1#-}" != "$1" ]; then
set -- php "$@"
fi
if [ "$GOSU_ENABLED" = "1" ]; then
exec gosu "${GOSU_UID}:${GOSU_GID}" "$@"
else
exec "$@"
fi
FROM php:8.2-cli
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
ENV GOSU_UID=1000
ENV GOSU_GID=1000
ENV GOSU_ENABLED=0
ENV COMPOSER_HOME=/.composer
RUN apt-get update && \
apt-get install -y zlib1g-dev libpng-dev libjpeg-dev libfreetype-dev libssl-dev libpq-dev libcurl4-openssl-dev \
libmemcached-dev libldap-dev \
libc-client-dev libkrb5-dev libxml2-dev libzip-dev libbz2-dev libffi-dev libxslt1-dev libmagickwand-dev \
gosu libcap2-bin acl git && \
docker-php-source extract && \
pecl install msgpack && pecl install xdebug && pecl install igbinary && pecl install pcov && \
docker-php-ext-install sockets && \
cd /usr/src/php/ext && \
pecl bundle redis && \
pecl bundle memcached && \
pecl bundle imagick && \
pecl bundle swoole && \
docker-php-ext-configure redis --enable-redis-igbinary --enable-redis-lzf && \
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-configure memcached --with-libmemcached-dir --with-zlib-dir --with-system-fastlz=no \
--enable-memcached-igbinary=yes --enable-memcached-msgpack=yes --enable-memcached-json=yes \
--enable-memcached-protocol=no --enable-memcached-sasl=yes --enable-memcached-session=yes && \
docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
docker-php-ext-configure imagick && \
docker-php-ext-configure swoole --enable-sockets=yes --enable-openssl=yes --enable-mysqlnd=yes --enable-swoole-curl=yes --enable-cares=no --enable-brotli=yes
RUN \
docker-php-ext-enable pcov igbinary msgpack && \
for ext in redis gd memcached pdo_pgsql ffi gettext ldap \
pcntl shmop sysvmsg sysvsem sysvshm xsl opcache imagick \
imap intl soap bcmath calendar exif mysqli pdo_mysql pgsql zip swoole; do \
docker-php-ext-install $ext || exit 1; done && \
mkdir "$COMPOSER_HOME" && setfacl -m d:m:rwx "$COMPOSER_HOME" && \
curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer && \
pear install PHP_CodeSniffer
# SWOOLE
RUN cd /usr/src/php/ext && \
pecl bundle swoole && \
docker-php-ext-configure swoole --enable-sockets=yes --enable-openssl=yes --enable-mysqlnd=yes --enable-swoole-curl=yes --enable-cares=no --enable-brotli=yes && \
docker-php-ext-install swoole
## CLEANUP
RUN \
apt-get -y autoremove && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
docker-php-source delete
RUN setcap 'cap_net_bind_service=+ep' /usr/local/bin/php
COPY docker-php-entrypoint /usr/local/bin/docker-php-entrypoint
COPY php.ini /usr/local/etc/php/php.ini
COPY phpcs.xml /var/www/phpcs.xml
RUN chmod +x /usr/local/bin/docker-php-entrypoint
WORKDIR /var/www/html
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["php", "-a"]
[PHP]
post_max_size = 100M
upload_max_filesize = 100M
variables_order = EGPCS
[opcache]
opcache.enable_cli=1
[Assertion]
zend.assertions = -1
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xsd">
<description>Local coding standards based on PSR2.</description>
<file>.</file>
<arg name="standard" value="PSR2"/>
<arg name="report" value="full"/>
<arg name="report-file" value="/var/www/html/docker/tmp/phpcs.report.log"/>
<arg value="nv"/>
<rule ref="PSR2" />
<exclude-pattern>/var/www/html/vendor/</exclude-pattern>
<exclude-pattern>/var/www/html/tests/resources/</exclude-pattern>
</ruleset>
#!/bin/bash
cur_path="$(dirname "$0")"
composer_file="$cur_path/docker-compose.yml"
if [ "$#" -eq 0 ]; then
set -- shell
fi
service="$1"
is_service() {
local service="$1"
local res=$(docker compose --file ./docker/docker-compose.yml config --format json | jq -r ".services.${service}" 2> /dev/null)
if [ "$res" = "null" ]; then
return 1
fi
return 0
}
checkCommand() {
command -v "$1" > /dev/null || exitWithErr "Missing dependency '$1'"
}
getServices() {
docker compose --file ./docker/docker-compose.yml config --format json | jq -crM --args ".services | keys"
}
showHelp() {
err "$(basename $0)"
err "=============="
err "Run commands in docker container"
err ""
err "Usage:"
err "------"
err -e "\t$0 build|rebuild|<service> [arg...]"
err -e "\t\t build: Build docker images from composer_file"
err -e "\t\trebuild: Build docker images from composer_file without cache"
err -e "\t\t<service>: One of the following: $(getServices)"
err ""
}
err() {
>&2 echo "$@"
}
exitWithErr() {
err "ERROR: $@"
exit 1
}
case $service in
build)
docker compose --progress=plain --project-name "$(basename $PWD)" --file ./docker/docker-compose.yml build 2>&1 | tee docker/tmp/build.log
;;
rebuild)
docker compose --progress=plain --project-name "$(basename $PWD)" --file ./docker/docker-compose.yml build --no-cache 2>&1 | tee docker/tmp/build.log
;;
help)
checkCommand jq
showHelp
;;
composer)
docker compose --project-name "$(basename $PWD)" --file "./docker/docker-compose.yml" run --rm php "$@"
;;
*)
is_service $service || exitWithErr "Service '$service' does not exist"
docker compose --project-name "$(basename $PWD)" --file "./docker/docker-compose.yml" run --rm "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment