Skip to content

Instantly share code, notes, and snippets.

@adrenalinkin
Last active February 6, 2023 16:27
Show Gist options
  • Save adrenalinkin/35176a7b52b996666c4a36384fd536ad to your computer and use it in GitHub Desktop.
Save adrenalinkin/35176a7b52b996666c4a36384fd536ad to your computer and use it in GitHub Desktop.
Simple docker-compose.yaml configuration for run any open source PHP project from the container

Supports PHP ^7.2 || ^8.0

Add into .gitignore_global folder .docker where we will locate our files

echo '.docker' >> ~/.gitignore_global

Add an alias to the host machine for more convenient use

echo 'alias docker-compose-bundled="docker-compose --env-file .docker/.env"' >> ~/.bash_profile
# only for Mac OS
echo 'alias docker-compose-bundled="docker-compose --env-file .docker/.env"' >> ~/.zshrc

Clone this Gist to the root folder of your project

git clone https://gist.github.com/35176a7b52b996666c4a36384fd536ad.git .docker

Set up your .env for the project

cp .docker/.env.dist .docker/.env

Set PROJECT_NAME name variable with unique name. Also, you can change PHP_VERSION and DOCKER_NETWORK if needed.

Now you can use docker

docker-compose-bundled build
docker-compose-bundled up -d
docker-compose-bundled exec php bash

For run the PHP-CS-Fixer

php-cs-fixer fix --diff

For run the composer-normalize

composer-normalize --dry-run
###> docker ###
COMPOSE_FILE=.docker/docker-compose.yaml
PHP_VERSION=8.1
PROJECT_NAME=unique_project_name
DOCKER_NETWORK=192.170.221.0/28
# Use for Linux
DOCKER_VOLUME_TYPE=none
DOCKER_VOLUME_OPTIONS=bind
DOCKER_VOLUME_DEVICE_PREFIX=
# Use for MAC
#DOCKER_VOLUME_TYPE=nfs
#DOCKER_VOLUME_OPTIONS=addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
#DOCKER_VOLUME_DEVICE_PREFIX=:
# for OSX Catalina
#DOCKER_VOLUME_TYPE=nfs
#DOCKER_VOLUME_OPTIONS=addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
#DOCKER_VOLUME_DEVICE_PREFIX=:/System/Volumes/Data
# Xdebug IDE config
IDE_SERVER_NAME=docker
# Use for Linux
XDEBUG_CONFIG=client_host=192.166.220.1
# Use for MAC
#XDEBUG_CONFIG=client_host=host.docker.internal
###< docker ###
###> tools ###
# For PHP ^8.0
TOOL_COMPOSER_NORMALIZE=*
TOOL_PHP_CS_FIXER=*
# For PHP ^7.2 || ^8.0
#TOOL_PHP_CS_FIXER=3.4.0
#TOOL_COMPOSER_NORMALIZE=2.19.0
###< tools ###
version: '3.7'
networks:
internal:
name: ${PROJECT_NAME}_network
driver: bridge
ipam:
driver: default
config:
- subnet: ${DOCKER_NETWORK}
volumes:
app_volume:
name: ${PROJECT_NAME}_app_volume
driver: local
driver_opts:
type: ${DOCKER_VOLUME_TYPE}
o: ${DOCKER_VOLUME_OPTIONS}
device: ${DOCKER_VOLUME_DEVICE_PREFIX}${PWD}
services:
php:
container_name: ${PROJECT_NAME}_php
image: ${PROJECT_NAME}_php
build:
context: .
args:
PHP_VERSION: ${PHP_VERSION}
TOOL_COMPOSER_NORMALIZE: ${TOOL_COMPOSER_NORMALIZE}
TOOL_PHP_CS_FIXER: ${TOOL_PHP_CS_FIXER}
networks:
- internal
volumes:
- app_volume:/app:rw
environment:
XDEBUG_CONFIG: ${XDEBUG_CONFIG}
PHP_IDE_CONFIG: serverName=${IDE_SERVER_NAME}
ARG PHP_VERSION
FROM composer:latest as composer
FROM php:${PHP_VERSION}-fpm as php-fpm
ARG HOME_DIR='/home/docker'
ARG TOOL_COMPOSER_NORMALIZE
ARG TOOL_PHP_CS_FIXER
RUN apt-get update && \
apt-get install -y --no-install-recommends \
gnupg2 \
libz-dev \
libcurl4-openssl-dev \
libzip-dev \
unzip \
vim \
wget \
zip
RUN docker-php-ext-configure zip
RUN docker-php-ext-install opcache zip
RUN pecl install pcov xdebug-3.1.6 # PHP 7.2 support
RUN docker-php-ext-enable opcache pcov
RUN echo "xdebug.mode=develop,debug,coverage" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.start_with_request = trigger" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.client_host = 172.17.0.1" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.client_port = 9003" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.discover_client_host = 0" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.idekey = PHPSTORM" >> /usr/local/etc/php/conf.d/xdebug.ini
RUN echo "pcov.enabled = 1" >> /usr/local/etc/php/conf.d/pcov.ini && \
echo "pcov.directory = /app/src" >> /usr/local/etc/php/conf.d/pcov.ini && \
echo "pcov.initial.memory = 1000000" >> /usr/local/etc/php/conf.d/pcov.ini && \
echo "pcov.initial.files = 100000" >> /usr/local/etc/php/conf.d/pcov.ini
# 0.14.5 compatible with php ^7.2 || ^8.0
RUN wget -O phive.phar https://github.com/phar-io/phive/releases/download/0.14.5/phive-0.14.5.phar && \
wget -O phive.phar.asc https://github.com/phar-io/phive/releases/download/0.14.5/phive-0.14.5.phar.asc && \
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 && \
gpg --verify phive.phar.asc phive.phar && \
chmod +x phive.phar && \
mv phive.phar /usr/local/bin/phive
RUN phive install --global --trust-gpg-keys E82B2FB314E9906E php-cs-fixer:$TOOL_PHP_CS_FIXER
RUN phive install --global --trust-gpg-keys C00543248C87FB13 ergebnis/composer-normalize:$TOOL_COMPOSER_NORMALIZE
RUN addgroup docker && \
adduser --ingroup docker --home ${HOME_DIR} --shell /bin/sh --disabled-password --gecos "" docker && \
mkdir -p $HOME_DIR/.local/bin
ENV PATH $HOME_DIR/.local/bin:$PATH
COPY --from=composer /usr/bin/composer $HOME_DIR/.local/bin
USER docker
RUN touch ${HOME_DIR}/.bash_aliases && \
echo 'alias phpx="php -dzend_extension=xdebug.so"' >> ${HOME_DIR}/.bash_aliases
WORKDIR /app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment