Skip to content

Instantly share code, notes, and snippets.

@njbair
Created September 16, 2022 18:18
Show Gist options
  • Save njbair/26f837c1f25a153497f74f49b1244eb0 to your computer and use it in GitHub Desktop.
Save njbair/26f837c1f25a153497f74f49b1244eb0 to your computer and use it in GitHub Desktop.
Docker PHP Template
############################
# ./docker-compose.yml #
############################
version: "3"
services:
php:
build: "./docker/php"
working_dir: /var/www/html
volumes:
- ${PWD}:/var/www/html
- ${PWD}/docker/php/php.ini:/usr/local/etc/php/php.ini
extra_hosts:
- "php.test:127.0.0.1"
hostname: "php.test"
domainname: test
ports:
- 80:80
environment:
DB_PORT: 3306
DB_HOST: database
database:
image: mariadb:latest
volumes:
- ${PWD}/docker/data/database:/var/lib/mysql
- ${PWD}/docker/sql:/docker-entrypoint-initdb.d
ports:
- 3306:3306
environment:
MARIADB_DATABASE: default
MARIADB_USER: default
MARIADB_PASSWORD: secret
MARIADB_ROOT_PASSWORD: secret
###############################
# ./docker/php/Dockerfile #
###############################
FROM php:8.1-apache
# update packages
RUN apt-get clean
RUN apt-get update
# configure Apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/site
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN a2enmod rewrite
# install MySQL
RUN apt-get install -y \
default-mysql-client \
&& docker-php-ext-install mysqli \
&& docker-php-ext-enable mysqli
# install Zip
RUN apt-get install -y \
libzip-dev \
zip \
&& docker-php-ext-install zip \
&& docker-php-ext-enable zip
# install additional packages & extensions
RUN apt-get install -y \
git
# install Composer
COPY install-composer.sh .
RUN bash install-composer.sh \
&& php -r "unlink('install-composer.sh');"
# copy development PHP ini file
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
#!/bin/bash
########################################
# ./docker/php/install-composer.sh #
########################################
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
mv composer.phar /usr/local/bin/composer
echo "PATH=\"$PATH:~/.composer/vendor/bin\"" >> ~/.bashrc
exit $RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment