Skip to content

Instantly share code, notes, and snippets.

@githubnando
Last active May 3, 2018 16:23
Show Gist options
  • Save githubnando/c9f03fcec23ecc41f5586dc66ae71d68 to your computer and use it in GitHub Desktop.
Save githubnando/c9f03fcec23ecc41f5586dc66ae71d68 to your computer and use it in GitHub Desktop.
Docker compose scaffold with MariaDB + PHP 7 + Composer + Yarn
# Docker compose environment scaffold using MariaDB and PHP 7, with Data Container for
# the database and without permission issues with mounted volumes (docker -v)
########################
###### Dockerfile ######
########################
```
FROM php:7.0.18-apache
# Basic tools and dependencies
RUN apt-get update && apt-get install apt-utils -y \
libssl-dev \
git \
zip \
unzip \
apt-transport-https
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin \
&& mv /usr/local/bin/composer.phar /usr/local/bin/composer
# Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update -o Dir::Etc::sourcelist="yarn.list" \
&& apt-get install -y yarn
# Default Virtual Host and apache configs
RUN sed -i "s/DocumentRoot .*/DocumentRoot \/var\/www\/html\/public/" /etc/apache2/sites-enabled/000-default.conf \
&& a2enmod rewrite \
&& a2enmod headers
# Project bootstraping
COPY bootstrap.sh /
RUN chmod +x /bootstrap.sh
CMD /bootstrap.sh
```
########################
#### Docker Compose ####
########################
```
version: '2'
services:
web:
build: .
ports:
- 8080:80
volumes:
- '.:/var/www/html'
environment:
- HOST_UID=${UID}
links:
- mariadb
mariadbdata:
image: mariadb:10.2
volumes:
- /var/lib/mysql
command: --break-maria
mariadb:
image: mariadb:10.2
volumes_from:
- mariadbdata
ports:
- "3306:3306"
environment:
# Only for development
- MARIADB_ROOT_PASSWORD=root
```
#############################
##### PROJECT BOOTSTRAP #####
#############################
```
#!/bin/bash
composer install
yarn install && yarn run build
# If it's a Laravel application
if [ ! -f .env ]; then
cp .env.example .env
php artisan key:generate
fi
function changeApacheUserId() {
getent group webserver &> /dev/null || groupadd -g $1 webserver
getent passwd webserver &> /dev/null || useradd -r -s /bin/false webserver -u $1 -g $1
export APACHE_RUN_USER=webserver
chown -R $1:$1 /var/www/html
}
if [[ "$HOST_UID" -gt 0 ]]; then
changeApacheUserId $HOST_UID
fi
apachectl stop
rm -vf /var/run/apache2/apache2.pid
apachectl -DFOREGROUND
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment