Skip to content

Commit

Permalink
Merge pull request #57 from usabilla/feature/split-debug-conf
Browse files Browse the repository at this point in the history
Improve development mode
  • Loading branch information
renatomefi authored Nov 2, 2018
2 parents 92b7e8a + 7169093 commit 14d192e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 60 deletions.
15 changes: 4 additions & 11 deletions Dockerfile-cli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN set -x \
# Patch CVE-2018-14618
RUN apk upgrade --no-cache curl

COPY src/php/conf/default.ini /usr/local/etc/php/conf.d/
COPY src/php/conf/ /usr/local/etc/php/conf.d/
COPY src/php/cli/conf/*.ini /usr/local/etc/php/conf.d/

# Install shush
Expand All @@ -45,16 +45,9 @@ HEALTHCHECK NONE
## CLI-DEV STAGE ##
FROM cli as cli-dev

# Enable XDebug
RUN set -x \
&& apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& pecl clear-cache \
&& docker-php-source-tarball clean \
&& apk del .phpize-deps

COPY src/php/conf/debug.ini /usr/local/etc/php/conf.d/zzz_debug.ini
# Install Xdebug and development specific configuration
RUN docker-php-dev-mode xdebug \
&& docker-php-dev-mode config

# Change entrypoint back to the default because we don't need shush in development
ENTRYPOINT ["docker-php-entrypoint"]
15 changes: 4 additions & 11 deletions Dockerfile-fpm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
# Patch CVE-2018-14618
RUN apk upgrade --no-cache curl

COPY src/php/conf/default.ini /usr/local/etc/php/conf.d/
COPY src/php/conf/ /usr/local/etc/php/conf.d/
COPY src/php/fpm/conf/*.conf /usr/local/etc/php-fpm.d/

# Install shush
Expand All @@ -48,16 +48,9 @@ VOLUME [ "/var/run" ]
## FPM-DEV STAGE ##
FROM fpm as fpm-dev

# Enable XDebug
RUN set -x \
&& apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& pecl clear-cache \
&& docker-php-source-tarball clean \
&& apk del .phpize-deps

COPY src/php/conf/debug.ini /usr/local/etc/php/conf.d/zzz_debug.ini
# Install Xdebug and development specific configuration
RUN docker-php-dev-mode xdebug \
&& docker-php-dev-mode config

# Change entrypoint back to the default because we don't need shush in development
CMD ["docker-php-entrypoint", "--force-stderr"]
58 changes: 28 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,44 @@ lint:
lint-shell:
docker run --rm -v ${current_dir}:/mnt:ro koalaman/shellcheck src/http/nginx/docker* src/php/utils/install-* src/php/utils/docker/* build*

DOCKER_TEST_RUN=docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose

test:
docker-compose -p php-docker-template-tests up --force-recreate --build -d \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_php_fpm_1' -m "php or php_fpm" \
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_fpm_1' \
-m "php or php_fpm or php_no_dev and not php_dev" \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_fpm_dev_1' \
-m "php or php_dev" \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_cli_1' \
-m "php or php_cli or php_no_dev and not php_dev" \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_php_cli_1' -m "php or php_cli" \
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_cli_dev_1' \
-m "php or php_cli or php_dev" \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_nginx_1' -m nginx \
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_nginx_1' \
-m "nginx" \
|| (docker-compose -p php-docker-template-tests down; echo "tests failed" && exit 1)
docker-compose -p php-docker-template-tests down

ci-test:
docker-compose -p php-docker-template-tests up --force-recreate -d
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_php_fpm_1' -m "php or php_fpm" --junitxml=/tests/test-results/php-fpm.xml
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_php_cli_1' -m "php or php_cli" --junitxml=/tests/test-results/php-cli.xml
docker run --rm -t \
--network php-docker-template-tests_backend-php \
-v "${current_dir}/test:/tests" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
renatomefi/docker-testinfra:1 --verbose --hosts='docker://php-docker-template-tests_nginx_1' -m nginx --junitxml=/tests/test-results/nginx.xml
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_fpm_1' \
-m "php or php_fpm or php_no_dev and not php_dev" --junitxml=/tests/test-results/php-fpm.xml
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_fpm_dev_1' \
-m "php or php_dev" --junitxml=/tests/test-results/php-fpm.xml
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_cli_1' \
-m "php or php_cli or php_no_dev and not php_dev" --junitxml=/tests/test-results/php-cli.xml
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_php_cli_dev_1' \
-m "php or php_cli or php_dev" --junitxml=/tests/test-results/php-cli.xml
$(DOCKER_TEST_RUN) --hosts='docker://php-docker-template-tests_nginx_1' \
-m nginx --junitxml=/tests/test-results/nginx.xml

scan-vulnerability:
docker-compose -f test/security/docker-compose.yml -p clair-ci up -d
Expand Down
15 changes: 14 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ services:
image: usabillabv/php:7.2-cli-alpine3.8
command:
- "-r"
- "while (true){};"
- "while (true){sleep(1);};"
networks:
- backend-php
php_cli_dev:
image: usabillabv/php:7.2-cli-alpine3.8-dev
command:
- "-r"
- "while (true){sleep(1);};"
networks:
- backend-php
php_fpm:
Expand All @@ -14,6 +21,12 @@ services:
- ./test/functional/web:/opt/project/public
networks:
- backend-php
php_fpm_dev:
image: usabillabv/php:7.2-fpm-alpine3.8-dev
volumes:
- ./test/functional/web:/opt/project/public
networks:
- backend-php
nginx:
image: usabillabv/php:nginx
ports:
Expand Down
5 changes: 5 additions & 0 deletions src/php/conf/available/dev.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
expose_php=on

opcache.validate_timestamps=1

zend.assertions=1
8 changes: 1 addition & 7 deletions src/php/conf/debug.ini → src/php/conf/available/xdebug.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
expose_php=on

opcache.validate_timestamps=1

xdebug.default_enable=0
xdebug.remote_enable=1
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_host=host.docker.internal
xdebug.remote_connect_back=0
xdebug.profiler_enable_trigger=1

zend.assertions=1
47 changes: 47 additions & 0 deletions src/php/utils/docker/docker-php-dev-mode
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
set -e

usage() {
echo "usage: $0 COMMAND"
echo
echo "Turns PHP into development mode."
echo "There's not option to revert it since this should only be executed for dev images or runtime debugging."
echo
echo "Commands:"
echo " xdebug downloads, installs and enable xdebug in the container"
echo " config adds standard development configuration for PHP"
echo
}

case "$1" in
xdebug)
apkDel=
if [ -n "$PHPIZE_DEPS" ]; then
if apk info --installed .phpize-deps-configure > /dev/null; then
apkDel='.phpize-deps-configure'
elif ! apk info --installed .phpize-deps > /dev/null; then
# shellcheck disable=SC2086
apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS
apkDel='.phpize-deps'
fi
else
>&2 echo "\$PHPIZE_DEPS env variable is necessary to run this script"
exit 1
fi

pecl install xdebug
docker-php-ext-enable xdebug
apk del $apkDel

cp /usr/local/etc/php/conf.d/available/xdebug.ini /usr/local/etc/php/conf.d/zzz_xdebug.ini
;;

config)
cp /usr/local/etc/php/conf.d/available/dev.ini /usr/local/etc/php/conf.d/zzz_dev.ini
;;

*)
usage
exit 1
;;
esac
35 changes: 35 additions & 0 deletions test/container/test_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

@pytest.mark.php_dev
def test_configuration_is_present(host):
assert host.file('/usr/local/etc/php/conf.d/zzz_xdebug.ini').exists is True
assert host.file('/usr/local/etc/php/conf.d/zzz_dev.ini').exists is True

@pytest.mark.php_dev
def test_configuration_is_effective(host):
configuration = host.run('php -i').stdout

assert u'expose_php => On => On' in configuration
assert u'opcache.validate_timestamps => On => On' in configuration
assert u'zend.assertions => 1 => 1' in configuration

@pytest.mark.php_dev
def test_xdebug_is_loaded(host):
assert 'Xdebug' in host.run('php -m').stdout

@pytest.mark.php_no_dev
def test_configuration_is_not_present(host):
assert host.file('/usr/local/etc/php/conf.d/zzz_xdebug.ini').exists is False
assert host.file('/usr/local/etc/php/conf.d/zzz_dev.ini').exists is False

@pytest.mark.php_no_dev
def test_configuration_is_not_effective(host):
configuration = host.run('php -i').stdout

assert u'expose_php => Off => Off' in configuration
assert u'opcache.validate_timestamps => Off => Off' in configuration
assert u'zend.assertions => -1 => -1' in configuration

@pytest.mark.php_no_dev
def test_xdebug_is_not_loaded(host):
assert 'Xdebug' not in host.run('php -m').stdout

0 comments on commit 14d192e

Please sign in to comment.