Skip to content

Instantly share code, notes, and snippets.

@harlesc
Last active March 14, 2017 13:03
Show Gist options
  • Save harlesc/64c3d6afe6a601ec5546 to your computer and use it in GitHub Desktop.
Save harlesc/64c3d6afe6a601ec5546 to your computer and use it in GitHub Desktop.
Docker + PHP 7.0.0RC6 + xdebug + docker-spy for Magento 1.9 development

Docker + PHP 7.0.0RC6 + xdebug + docker-spy for Magento 1.9 development

This gist contains hints/tips to get you started using Docker with PHP7 and xdebug including a local DNS solution (docker-spy).

Look at the Dockerfile, the docker.sh script just builds/runs to suit my specs, hope it helps you out!

I did this on a Mint 17/Ubuntu 14.04 machine using Docker version 1.7.0, build 0baf609.

Local DNS for Docker Containers using docker-spy

1) Edit /etc/resolv.conf

Your mileage may vary on this step, read this first:
http://www.ivoverberk.nl/service-discovery-with-docker/

I had to add this line (Mint 17/Ubuntu 14.04):

nameserver 127.0.0.1  

You can also edit /etc/default/docker (...or it could be /etc/docker/docker.io depending how you installed docker) to add ---dns options to suit your environment.

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

http://docs.docker.com/engine/articles/configuring/#configuring-docker

Mint 17/Ubuntu 14.04 complicates things because it uses dnsmasq, just something to keep in mind.

2) Setup docker-spy

docker pull iverberk/docker-spy

# create the docker-spy container, I needed the localhost "127.0.0.1" IP due to Mint17s use of dnsmasq, complicates things.  
docker run --name docker_spy -p 127.0.0.1:53:53/udp -p 127.0.0.1:53:53 -v /var/run/docker.sock:/var/run/docker.sock iverberk/docker-spy

# Note: need to use 'docker run' like this when starting containers to use docker-spy:
# docker run --dns=172.17.42.1 -h agent1.localdomain --rm -it centos:centos6 bash  
# docker run --dns=172.17.42.1 -h agent2.localdomain --rm -it centos:centos6 bash  
# -h, --hostname=""
# --dns=[] Set custom DNS servers

###3) modify/run docker.sh

Charles Hilditch info@magemechanic.com
http://www.magemechanic.com/

#!/usr/bin/env bash
# Charles Hilditch <info@magemechanic.com>
# docker build/run php:7-apache container for Magento 1.9.* using docker-spy for local DNS, assumes MySQL running on docker host.
# could probably do this again in PHP and move into an n98-magerun add-on
# modify to suit your needs.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CONF_PATH=$DIR/../www # change this, path to magento web root
# dependencies available?
if ! type 'xmlstarlet' > /dev/null 2>&1 || \
! type 'modman' > /dev/null || \
! type 'n98-magerun.phar' > /dev/null; then
echo "Error: install missing dependencies."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return || exit;
fi
if [ ! -e $CONF_PATH ]; then
echo "No www directory or symlink. Aborting."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return || exit;
fi
# Change this (I use the project directory name to create the docker container name and hostname
# assumes path is like this: PROJECT_NAME/some_script_dir/docker.sh)
# docker name must be [a-z0-9-_.] also used for hostname
PROJECT_NAME=$(echo $(basename $(dirname $(pwd))) | tr '[:upper:]' '[:lower:]' | sed 's/-//g' )-$(date +%s)
echo "Project name: "$PROJECT_NAME;
DOMAIN_NAME=$PROJECT_NAME.localdomain # by default, docker-spy routes for domains matching 'localdomain'
echo "Build docker container"
if ! docker build -t $PROJECT_NAME $DIR; then
echo "docker build failed. Aborting."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return || exit;
fi
echo "Run docker container, hostname/domain name: $DOMAIN_NAME"
GATEWAY_IP_ADDR=$(ifconfig docker0 | awk '/inet addr/{print substr($2,6)}')
CONTAINER_ID=$(docker run --dns=$GATEWAY_IP_ADDR -h "$DOMAIN_NAME" -d -v $DIR/../www:/var/www/html --name $PROJECT_NAME $PROJECT_NAME)
if [ "$CONTAINER_ID" = "" ]; then
echo "docker run failed. Aborting."
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return || exit;
fi
# IP of container
CONTAINER_IP_ADDR=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" $CONTAINER_ID)
# xmlstarlet sel --text -t -v '/config/global/resources/default_setup/connection/host' $DIR/../www/app/etc/local.xml
echo "Update local.xml with Gateway IP Address"
xmlstarlet ed --inplace --update '/config/global/resources/default_setup/connection/host' -v $GATEWAY_IP_ADDR $DIR/../www/app/etc/local.xml
# If running MySQL on docker host, may need to allow MySQL connections from container to host. ubuntu/mint host:
#
# sudo ufw allow 3306/tcp
# sudo service ufw restart
#
# Also change 'bind-address' from "bind-address = 127.0.0.1" to "bind-address = 0.0.0.0" in /etc/mysql/my.cnf, service mysql restart
# grant ALL on `DB_NAME`.* to 'USER_NAME'@'%' identified by 'PASSWORD';
echo "set magento domain"
n98-magerun.phar --root-dir=$CONF_PATH config:set 'web/unsecure/base_url' "http://$DOMAIN_NAME/"
n98-magerun.phar --root-dir=$CONF_PATH config:set 'web/secure/base_url' "http://$DOMAIN_NAME/"
n98-magerun.phar --root-dir=$CONF_PATH config:set 'web/cookie/cookie_domain' "$DOMAIN_NAME"
n98-magerun.phar --root-dir=$CONF_PATH config:set 'web/cookie/cookie_path' '/'
pushd .
# make sure Magento 1.9.2.2 works with PHP7, install MageMechanic_PhpSeven using modman
cd $CONF_PATH && modman init >/dev/null 2>&1 && modman clone https://github.com/MageMechanic/PhpSeven.git >/dev/null;
popd
# allow symlinks
n98-magerun.phar --root-dir=$CONF_PATH config:set dev/template/allow_symlink 1
echo "clear magento cache"
n98-magerun.phar --root-dir=$CONF_PATH cache:flush
echo "done."
echo "docker exec /bin/bash..."
docker exec -it $CONTAINER_ID /bin/bash
# Charles Hilditch <info@magemechanic.com>
# Tested on Magento 1.9.2.2 with MageMechanic_PhpSeven module installed for PHP7 compatibility.
# https://github.com/MageMechanic/PhpSeven
FROM php:7-apache
RUN apt-get update && apt-get install -y vim libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12-dev libicu-dev mlocate
RUN chown -R www-data:www-data /var/www/html
RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/lib/x86_64-linux-gnu
RUN docker-php-ext-install gd mysqli pdo pdo_mysql mcrypt intl
RUN cp /usr/src/php/php.ini-development /usr/local/etc/php/php.ini && sed -i.bak 's/\;error_log = php_errors.log/error_log = \/var\/log\/php_errors.log/' /usr/local/etc/php/php.ini && touch /var/log/php_errors.log
RUN a2enmod rewrite && apachectl restart
# xdebug-2.4.0beta1 supports PHP7 (boom!)
RUN touch /usr/local/etc/php/conf.d/xdebug.ini; \
echo xdebug.remote_enable=1 >> /usr/local/etc/php/conf.d/xdebug.ini; \
echo xdebug.remote_autostart=0 >> /usr/local/etc/php/conf.d/xdebug.ini; \
echo xdebug.remote_connect_back=1 >> /usr/local/etc/php/conf.d/xdebug.ini; \
echo xdebug.remote_port=9000 >> /usr/local/etc/php/conf.d/xdebug.ini; \
echo xdebug.remote_log=/tmp/php5-xdebug.log >> /usr/local/etc/php/conf.d/xdebug.ini;
RUN mkdir ~/software && \
cd ~/software/ && \
apt-get install -y wget && \
wget http://xdebug.org/files/xdebug-2.4.0beta1.tgz && \
tar -xvzf xdebug-2.4.0beta1.tgz && \
cd xdebug-2.4.0beta1 && \
phpize && \
./configure && \
make && \
cp modules/xdebug.so /usr/local/lib/php/extensions/no-debug-non-zts-20151012 && \
echo "zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so" >> /usr/local/etc/php/php.ini && \
apachectl graceful
# # If you need the plain old 'mysql' extension in php7 for legacy reasons, uncomment below
# RUN mkdir -p ~/software && \
# cd ~/software && \
# apt-get install git -y && \
# git clone https://github.com/php/pecl-database-mysql mysql --recursive && \
# cd mysql && \
# phpize && \
# ./configure && \
# make && \
# make install && \
# echo "extension = /usr/local/lib/php/extensions/no-debug-non-zts-20141001/mysql.so" >> /usr/local/etc/php/php.ini && \
# apachectl graceful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment