Last active
June 6, 2019 12:10
-
-
Save iraklisg/9eaea291a2669484fe741a4584a42441 to your computer and use it in GitHub Desktop.
Dockerize Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ########################################################## | |
# Stuff related to docker development | |
# ########################################################## | |
docker-compose.* | |
# ########################################################## | |
# Application related stuff | |
# ########################################################## | |
node_modules/ | |
vendor/ | |
.idea/ | |
.git/ | |
.env* | |
storage/framework/cache/** | |
storage/framework/sessions/** | |
storage/framework/views/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ############################################################################# | |
# | |
# NAME | |
# deploy.sh | |
# | |
# SYNOPSIS | |
# ./deploy [OPTIONS] | |
# | |
# DESCRIPTION | |
# This is a helper script that facilitates deployment of production-ready | |
# docker images | |
# | |
# OPTIONS | |
# down Remove the running stack | |
# | |
# REQUIREMENTS | |
# Docker must be pre-installed and docker-swarm mode must be enabled | |
# | |
# BUGS: --- | |
# NOTES: --- | |
# AUTHOR: --- | |
# COMPANY: --- | |
# VERSION: --- | |
# CREATED: --- | |
# REVISION: --- | |
# ############################################################################# | |
# Global variables | |
STACK="" # Enter the stack name | |
IMAGE="" # Enter the image name | |
# If we pass any arguments... | |
if [[ $# -gt 0 ]];then | |
# Build the docker images | |
if [[ "$1" == "down" ]]; then | |
shift 1 | |
docker stack rm ${STACK} | |
fi | |
else | |
echo -e "\n\033[036m ✔ Attempt to pull the required images\033[0m\n" | |
echo "y" | docker login | |
docker pull ${IMAGE} -a # TODO correct stack-file to always use *latest* tag | |
# TODO Make some checks | |
echo -e "\n\033[036m ✔ All checks passed; ready to deploy the stack\033[0m\n" | |
# Deploy the stack using the stack-files | |
docker stack deploy -c docker-compose.yml -c docker-compose.prod.yml ${STACK} | |
# If experiencing problems when trying to automatically pull images, use --with-registry-auth | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ############################################################################# | |
# | |
# NAME | |
# develop.sh | |
# | |
# SYNOPSIS | |
# ./develop [OPTIONS] [..ARGS] | |
# | |
# DESCRIPTION | |
# This is a helper script that facilitates development via docker | |
# | |
# OPTIONS | |
# build Build the images | |
# artisan Run a php artisan command | |
# test Run a phpunit tests | |
# composer Run a composer command | |
# npm Run an npm command | |
# <CMD> Pass-through any command to docker-compose | |
# | |
# REQUIREMENTS | |
# Docker and Docker Compose must been preinstalled | |
# | |
# BUGS: --- | |
# NOTES: --- | |
# AUTHOR: --- | |
# COMPANY: --- | |
# VERSION: --- | |
# CREATED: --- | |
# REVISION: --- | |
# ############################################################################# | |
COMPOSE="docker-compose" | |
USER_ID=$(id -u) | |
GROUP_ID=$(id -g) | |
DOCKER_USERNAME="" # your docker username | |
# If we pass any arguments... | |
if [[ $# -gt 0 ]];then | |
# Build the docker images | |
if [[ "$1" == "build" ]]; then | |
shift 1 | |
${COMPOSE} build "$@" | |
COMMIT_VESION=$(git rev-parse --short HEAD) | |
TARGET_IMAGE=$(${COMPOSE} config | grep -F "image: ${DOCKER_USERNAME}" | cut -d':' -f2,3) | |
IMAGE_REPOSITORY=$(echo ${TARGET_IMAGE} | cut -d':' -f1) | |
IMAGE_TAG=$(echo ${TARGET_IMAGE} | cut -d':' -f2) | |
docker image tag ${TARGET_IMAGE} ${IMAGE_REPOSITORY}:${COMMIT_VESION} | |
echo "Successfully tagged ${IMAGE_REPOSITORY}:${COMMIT_VESION}" | |
docker image tag ${TARGET_IMAGE} ${IMAGE_REPOSITORY}:latest | |
echo "Successfully tagged ${IMAGE_REPOSITORY}:latest" | |
# Run an artisan command | |
elif [[ "$1" == "artisan" ]]; then | |
shift 1 | |
if [[ "$@" == make:* || "$@" == vendor:* ]]; then | |
${COMPOSE} run --rm \ | |
-w /var/www/html \ | |
--user ${USER_ID}:${GROUP_ID} \ | |
app \ | |
php artisan "$@" | |
else | |
${COMPOSE} run --rm \ | |
-w /var/www/html \ | |
app \ | |
php artisan "$@" | |
fi | |
# Run PHPUnit tests | |
elif [[ "$1" == "test" ]]; then | |
shift 1 | |
${COMPOSE} -f docker-compose.yml -f docker-compose.testing.yml \ | |
run --rm \ | |
-w /var/www/html \ | |
app \ | |
./vendor/bin/phpunit "$@" ; \ | |
${COMPOSE} -f docker-compose.yml -f docker-compose.testing.yml \ | |
stop \ | |
mysql-testing | |
elif [[ "$1" == "test:migrate" ]]; then | |
shift 1 | |
${COMPOSE} -f docker-compose.yml -f docker-compose.testing.yml \ | |
run --rm \ | |
-w /var/www/html \ | |
app \ | |
php artisan migrate:refresh | |
# Run a composer command | |
elif [[ "$1" == "composer" ]]; then | |
shift 1 | |
if [[ "$@" == install || "$@" == update ]]; then | |
${COMPOSE} run --rm \ | |
-w /app \ | |
--user ${USER_ID}:${GROUP_ID} \ | |
composer \ | |
"$@" \ | |
--ignore-platform-reqs \ | |
--no-plugins \ | |
--no-scripts \ | |
--optimize-autoloader \ | |
--no-suggest \ | |
--ansi \ | |
--no-interaction | |
else | |
${COMPOSE} run --rm \ | |
-w /app \ | |
--user ${USER_ID}:${GROUP_ID} \ | |
composer \ | |
"$@" \ | |
--ansi | |
fi | |
# Run npm command | |
elif [[ "$1" == "npm" ]]; then | |
shift 1 | |
${COMPOSE} run --rm \ | |
-w /app \ | |
--user ${USER_ID}:${GROUP_ID} \ | |
node \ | |
npm "$@" | |
# Else, pass-thru args directly to docker-compose | |
else | |
${COMPOSE} "$@" | |
fi | |
else | |
${COMPOSE} ps | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# This is the DEV docker-compose file. | |
# | |
# USAGE | |
# Execute via: | |
# - develop script (recommended) | |
# - docker-compose CLI | |
# ############################################################################# | |
version: '3.7' | |
services: | |
################################### | |
# Application service | |
################################### | |
app: | |
build: | |
context: ./ | |
dockerfile: ./docker/app/Dockerfile | |
environment: | |
- APP_ENV=local | |
depends_on: | |
- mysql | |
ports: | |
- ${APP_PORT}:80 | |
networks: | |
- backend-dev | |
volumes: | |
- .:/var/www/html | |
################################### | |
# Database service | |
################################### | |
mysql: | |
environment: | |
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} | |
- MYSQL_DATABASE=${DB_DATABASE} | |
- MYSQL_USER=${DB_USERNAME} | |
- MYSQL_PASSWORD=${DB_PASSWORD} | |
ports: | |
- ${DB_PORT}:3306 | |
networks: | |
- backend-dev | |
volumes: | |
- mysql-dev:/var/lib/mysql | |
################################### | |
# PHP composer | |
################################### | |
composer: | |
image: composer:1.8.0 # version must match the one in dockerfile | |
volumes: | |
- .:/app | |
################################### | |
# Node/npm | |
################################### | |
node: | |
image: node:10.14.2 | |
volumes: | |
- .:/app | |
################################### | |
# Networks | |
################################### | |
networks: | |
backend-dev: | |
driver: bridge | |
################################### | |
# Volumes | |
################################### | |
volumes: | |
mysql-dev: | |
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# This is the PROD docker-compose file | |
# | |
# USAGE | |
# Execute via: | |
# - deploy script (recommended) | |
# - docker stack deploy -c docker-compose.yml -c docker-compose.prod.yml <STACK_NAME> | |
# | |
# REQUIREMENTS | |
# The docker secrets must been already defined via `docker secret create` | |
# ############################################################################# | |
version: '3.7' | |
services: | |
################################### | |
# Application service | |
################################### | |
app: | |
environment: | |
- APP_ENV=production | |
- APP_DEBUG=false | |
secrets: | |
- env_prod | |
ports: | |
- 8081:80 | |
networks: | |
- backend-prod | |
################################### | |
# Database service | |
################################### | |
mysql: | |
environment: | |
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_root_passwd | |
- MYSQL_DATABASE_FILE=/run/secrets/mysql_database | |
- MYSQL_USER_FILE=/run/secrets/mysql_user | |
- MYSQL_PASSWORD_FILE=/run/secrets/mysql_passwd | |
secrets: | |
- mysql_root_passwd | |
- mysql_passwd | |
- mysql_database | |
- mysql_user | |
# Allow remote connections to mysql server (e.g via mysql-workbench) | |
# ports: | |
# - 3306:3306 | |
networks: | |
- backend-prod | |
volumes: | |
- mysql-prod:/var/lib/mysql | |
# Allow remote connections to mysql server (e.g via mysql-workbench) | |
# - ./docker/db/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf | |
################################### | |
# Networks | |
################################### | |
networks: | |
backend-prod: | |
driver: overlay | |
################################### | |
# Volumes | |
################################### | |
volumes: | |
mysql-prod: | |
driver: local | |
################################### | |
# Secrets | |
################################### | |
secrets: | |
env_prod: | |
external: true | |
mysql_root_passwd: | |
external: true | |
mysql_passwd: | |
external: true | |
mysql_database: | |
external: true | |
mysql_user: | |
external: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# This is the testing docker-compose file | |
# ############################################################################# | |
version: '3.7' | |
services: | |
################################### | |
# Laravel application service | |
################################### | |
app: | |
environment: | |
- APP_ENV=testing | |
- DB_HOST=mysql-testing | |
depends_on: | |
- mysql-testing | |
networks: | |
- backend-testing | |
volumes: | |
- .:/var/www/html | |
################################### | |
# Testing Database service | |
################################### | |
mysql-testing: | |
image: mysql:5.7 | |
environment: | |
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} | |
- MYSQL_DATABASE=${DB_DATABASE} | |
- MYSQL_USER=${DB_USERNAME} | |
- MYSQL_PASSWORD=${DB_PASSWORD} | |
ports: | |
- 3309:3306 | |
networks: | |
- backend-testing | |
volumes: | |
- mysql-testing:/var/lib/mysql | |
################################### | |
# Networks configuration reference | |
################################### | |
networks: | |
backend-testing: | |
driver: bridge | |
################################### | |
# Volumes configuration reference | |
################################### | |
volumes: | |
mysql-testing: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# This is the base docker-compose file. | |
# ############################################################################# | |
version: '3.7' | |
services: | |
################################### | |
# Application service | |
################################### | |
app: | |
image: iraklisg/myapp:0.1.0 | |
################################### | |
# Database service | |
################################### | |
mysql: | |
image: mysql:8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# This is a pretty standard PHP configuration for Nginx. Note that it's using | |
# the local unix socket file instead of a TCP network address. | |
# Since PHP-FPM and Nginx will be within the same container, we can use the | |
# slightly faster unix socket for communication between Nginx and PHP-FPM. | |
# ############################################################################# | |
server { | |
listen 80 default_server; | |
root /var/www/html/public; # The document root | |
index index.html index.htm index.php; | |
server_name _; | |
charset utf-8; | |
location = /favicon.ico { log_not_found off; access_log off; } | |
location = /robots.txt { log_not_found off; access_log off; } | |
location / { | |
try_files $uri $uri/ /index.php$is_args$args; | |
} | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
# Use a unix socket, i.e expect php-fpm to live in the same container as nginx | |
# and create to the php7.3-fpm.sock file within container's /run/php directory | |
fastcgi_pass unix:/run/php/php7.3-fpm.sock; | |
} | |
error_page 404 /index.php; | |
location ~ /\.ht { | |
deny all; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ############################################################################# | |
# DESCRIPTION | |
# The ENTRYPOINT script bootstraps the `app` service. | |
# Depending on the environment defined via the ${APP_ENV} environment variable | |
# declared on the `docker-compose.*.yml` file, the entrypoint script will | |
# perform various actions such like parsing the .env file, initializing | |
# the database, etc.. | |
# | |
# In production environment: | |
# 1. Creates the required .env file for production from the `env_prod` | |
# secret file | |
# 2. Creates the required databases and grand permissions by reading | |
# the contents of the created .env file | |
# 3. Automatically runs all required database migrations TODO think again | |
# if this is a good practise... | |
# | |
# In development environment | |
# 1. Creates the required databases and grand permissions by reading the | |
# contents of the created .env file | |
# | |
# REQUIREMENTS | |
# In order the entrypoint to bootstrap app service in production environment, | |
# a valid docker secret file named `env_prod` must exists | |
# ############################################################################# | |
set -e | |
# ############################################################################# | |
# Create the required .env file (only in PRODUCTION) | |
# ############################################################################# | |
# | |
if [[ ${APP_ENV} == "production" ]]; then | |
# If a env-prod file exists as a Docker Secret, | |
# we'll use that instead of the one potentially included | |
echo -e "\n\033[036m *********** PRODUCTION ENVIRONMENT DETECTED *********** \n 🔑 Try creating a .env file from docker secret\033[0m\n" | |
if [[ -f /run/secrets/env_prod ]]; then | |
# Delete .env file if it exists | |
if [[ -f /var/www/html/.env ]]; then | |
rm /var/www/html/.env | |
fi | |
# Symlink .env from the secret file | |
ln -sf /run/secrets/env_prod /var/www/html/.env | |
echo -e "\n\033[032m ✔ ️The .env file has been created from docker secret \033[0m\n" | |
else | |
echo -e "\n\033[031m ✗ Cannot create the required .env file; <env_prod> secret does not exist \033[0m\n" | |
exit 1 | |
fi | |
fi | |
# ############################################################################# | |
# Prepare MySQL databases | |
# ############################################################################# | |
# | |
# On docker image runtime, we want the app service to do the following: | |
# - Parse .env file to grab the required `mysql` connection parameters | |
# - Check that the db service is up and ready | |
# - Create the required databases | |
# - Grant db user all privileges on required databases | |
# | |
PHP_OUT=$(php << 'EOPHP' | |
<?php | |
$stdout = fopen('php://stdout', 'w'); // Make STDOUT resource writable | |
$stderr = fopen('php://stderr', 'w'); // Make STDERR resource writable | |
$envFilePath = '/var/www/html/.env'; // The path of the .env file inside the container | |
/* | |
|-------------------------------------------------------------------------- | |
| Grab database parameters | |
|-------------------------------------------------------------------------- | |
| | |
| Parse the .env file to grab the required parameters to connect to database | |
| | |
*/ | |
// Parse the contents of .env file to an array | |
try { | |
$envArr = envToArray($envFilePath); | |
} catch (\Exception $e) { | |
fwrite($stderr, "\n\033[31m ✗ Failed to parse the .env file to an Array: \n$e \033[0m\n"); | |
exit(1); | |
} | |
// `DB_HOST` value must be explicitly overwritten in case of testing | |
// environment because we are using a different service name. | |
if (getenv('APP_ENV') === 'testing') { | |
$envArr['DB_HOST'] = 'mysql-testing'; | |
} | |
// Grab the required parameters for connecting to `mysql` service | |
$port = 3306; // mysql server port number | |
$host = $envArr['DB_HOST']; // mysql server host name | |
$root_passwd = $envArr['DB_ROOT_PASSWORD']; // mysql root password | |
$user = $envArr['DB_USERNAME']; // mysql user | |
$passwd = $envArr['DB_PASSWORD']; // mysql user password | |
$db = $envArr['DB_DATABASE_MAIN']; // mysql default database (created by mysql docker image) | |
/* | |
|-------------------------------------------------------------------------- | |
| Check database connection | |
|-------------------------------------------------------------------------- | |
| Make sure that we can connect to database (just to be safe) | |
| The database service requires a few minutes to be up and ready | |
| See also https://github.com/docker-library/wordpress/blob/master/docker-entrypoint.sh | |
| | |
*/ | |
$maxTries = 10; // Max number of tries to connect to MySQL server | |
do { | |
// We use the same credentials used for mysql service instantiation | |
// IMPORTANT NOTES: | |
// - The $DB_USER created by mysql service does not have sufficient privileges, so we try to connect as root | |
// - We need to re-instantiate the connection every time it fails | |
$conn = new mysqli($host, 'root', $root_passwd); | |
if ($conn->connect_error) { | |
fwrite($stderr, "\n\033[31m ✗ MySQL Connection Error: (".$conn->connect_errno .") -".$conn->connect_error.".\nTry again in a few seconds...\033[0m\n"); | |
--$maxTries; | |
if ($maxTries <= 0) { | |
exit(1); | |
} | |
sleep(10); | |
} | |
} while ($conn->connect_error); | |
fwrite($stdout, "\033[32m ✔ Successfully connect to mysql server <$host:$port>\033[0m\n"); | |
/* | |
|-------------------------------------------------------------------------- | |
| Helper functions | |
|-------------------------------------------------------------------------- | |
| | |
*/ | |
/** | |
* Parses the .env file to an array | |
* @throws Exception | |
* | |
* @link https://github.com/jsefton/php-dotenv-parser/blob/master/src/Parser.php | |
*/ | |
function envToArray($envPath) { | |
$envArr = []; | |
$content = file_get_contents($envPath); | |
if ($content == false) { | |
throw new Exception("Failed to get the contents of .env file"); | |
} | |
$lines = preg_split('/\n|\r\n?/', $content); // $lines = explode("\r", $content); | |
if($lines) { | |
foreach($lines as $line) { | |
$entry = trim($line); | |
// If not an empty entry and not a comment then parse entry | |
if(strlen($entry) > 0 && $entry[0] !== "#") { | |
// Find position of first equals symbol | |
$equalsLocation = strpos($entry, '='); | |
// Pull everything to the left of the first equals | |
$key = trim(substr($entry, 0, $equalsLocation)); | |
// Pull everything to the right from the equals to end of the entry | |
$value = trim(substr($entry, ($equalsLocation + 1), strlen($entry))); | |
$envArr[$key] = $value; | |
} | |
} | |
} else { | |
throw new Exception("No content found; .env appears to be empty"); | |
} | |
return $envArr; | |
} | |
?> | |
EOPHP | |
) | |
# All success messages written in STDOUT will be concatenated and assigned as a value to the PHP_OUT bash variable. | |
echo -e "\n$PHP_OUT\n" | |
# ############################################################################# | |
# Run migrations (only in PRODUCTION) | |
# ############################################################################# | |
# | |
# If we are in *production* environment, run migrations automatically | |
# every time we start (initialize) a fresh container | |
# | |
# If we are in *local* environment, do not run migrations automatically | |
# | |
# | |
if [[ ${APP_ENV} == "production" ]]; then | |
# Do some stuff only in a fresh container | |
if [[ ! -f .initialized ]]; then # checks whether it is run in a fresh container | |
php artisan migrate --force | |
echo -e "\n\033[032m ✔ All migrations applied successfully \033[0m\n" | |
touch .initialized # subsequent container starts won't execute the initialization command again | |
fi | |
fi | |
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################################################################# | |
# DESCRIPTION | |
# Dockerfile to build APPTREE image. It creates a multistage build: | |
# - Stage 1: Install Composer Dependencies | |
# - Stage 2: Install and configure nginx, php-fpm and supervisor and | |
# copy our app code and artifacts from the first stages | |
# ############################################################################# | |
FROM composer:1.8.0 as vendor | |
COPY composer.json composer.loc[k] /app/ | |
RUN composer install \ | |
--ignore-platform-reqs \ | |
--no-plugins \ | |
--no-scripts \ | |
--no-autoloader \ | |
--no-suggest \ | |
--ansi \ | |
--no-interaction | |
COPY . /app/ | |
RUN composer dump-autoload -o | |
FROM ubuntu:18.04 | |
LABEL maintainer = "ira" | |
ARG NGINX_VERSION=1.14.* | |
ARG PHP_VERSION=7.3 | |
# | |
#-------------------------------------------------------------------------- | |
# APT Dependencies Installation | |
#-------------------------------------------------------------------------- | |
# | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends --no-install-suggests -y \ | |
curl \ | |
zip \ | |
unzip \ | |
git \ | |
software-properties-common \ | |
supervisor \ | |
locales \ | |
&& locale-gen el_GR.UTF-8 # generate and set the correct locales for the application to work properly (dompdf, etc...) | |
# Boilerplate required by ppa:ondrej/php repositiry | |
ENV LANG=el_GR.UTF-8 \ | |
LANGUAGE=el_GR:el \ | |
LC_ALL=el_GR.UTF-8 | |
# Avoiding user interaction with tzdata when installing certbot in a docker container | |
ENV DEBIAN_FRONTEND=noninteractive | |
# | |
#-------------------------------------------------------------------------- | |
# NGINX Installation | |
#-------------------------------------------------------------------------- | |
# | |
ENV NGINX_VERSION=$NGINX_VERSION | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends --no-install-suggests -y \ | |
nginx=${NGINX_VERSION} | |
# | |
#-------------------------------------------------------------------------- | |
# PHP Installation | |
#-------------------------------------------------------------------------- | |
# | |
ENV PHP_VERSION=$PHP_VERSION | |
RUN add-apt-repository -y ppa:ondrej/php \ | |
&& apt-get update \ | |
&& apt-get install --no-install-recommends --no-install-suggests -y \ | |
php${PHP_VERSION} \ | |
php${PHP_VERSION}-fpm \ | |
php${PHP_VERSION}-cli \ | |
php${PHP_VERSION}-gd \ | |
php${PHP_VERSION}-mysql \ | |
php${PHP_VERSION}-imap \ | |
php${PHP_VERSION}-mbstring \ | |
php${PHP_VERSION}-xml \ | |
php${PHP_VERSION}-curl \ | |
php${PHP_VERSION}-ldap \ | |
php${PHP_VERSION}-zip \ | |
php-memcached | |
# | |
#-------------------------------------------------------------------------- | |
# NGINX Configuration | |
#-------------------------------------------------------------------------- | |
# | |
# Overwrite the default nginx configuration and make sure it is setup for php-fpm | |
# Make nginx run on the foreground (not as deamon, required by supervisor) and | |
# forward nginx request and error logs to docker log collector (stderr) | |
COPY ./docker/app/default /etc/nginx/sites-available/default | |
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \ | |
&& ln -sf /dev/stdout /var/log/nginx/access.log \ | |
&& ln -sf /dev/stderr /var/log/nginx/error.log | |
# | |
#-------------------------------------------------------------------------- | |
# PHP-FPM Configuration | |
#-------------------------------------------------------------------------- | |
# | |
# Overwrite the php-fpm default configuration to run in foreground and forward errors to stderr | |
RUN mkdir /run/php | |
COPY ./docker/app/php-fpm.conf /etc/php/${PHP_VERSION}/fpm/php-fpm.conf | |
# | |
#-------------------------------------------------------------------------- | |
# Supervisor Configuration | |
#-------------------------------------------------------------------------- | |
# | |
# Overwirte the supervisor default configuration to tell him how to run nginx and php-fpm | |
COPY docker/app/supervisord.conf /etc/supervisor/conf.d/supervisord.conf | |
# | |
#-------------------------------------------------------------------------- | |
# Final Touch | |
#-------------------------------------------------------------------------- | |
# | |
# Clean up installation artifacts to make image leaner | |
RUN apt-get remove -y --purge software-properties-common \ | |
&& apt-get -y autoremove \ | |
&& apt-get clean \ | |
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | |
# Copy the application code and application dependencies | |
WORKDIR /var/www/html | |
COPY --from=vendor /app/vendor ./vendor | |
COPY . ./ | |
COPY docker/app/docker-entrypoint.sh /usr/local/bin/ | |
# Configure Laravel directory permissions | |
RUN chown -R www-data:www-data /var/www/html/bootstrap/cache \ | |
&& chown -R www-data:www-data /var/www/html/storage \ | |
&& chmod +x /usr/local/bin/docker-entrypoint.sh | |
EXPOSE 80 | |
# On RUNTIME check if new schemas and/or migrations need to be applied | |
# ENTRYPOINT ["docker-entrypoint.sh"] | |
# Start the container with the following command | |
CMD ["/usr/bin/supervisord"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ---------------------------------------------------------------------------------- | |
; We need to make two changes to the default php-fom.conf file: | |
; | |
; daemonize = no - Run PHP-FPM in the foreground, so Supervisord can supervise it | |
; error_log = /proc/self/fd/2 - Send php-fpm error log output to stderr | |
; ---------------------------------------------------------------------------------- | |
;;;;;;;;;;;;;;;;;;;; | |
; FPM Configuration ; | |
;;;;;;;;;;;;;;;;;;;;; | |
; All relative paths in this configuration file are relative to PHP's install | |
; prefix (/usr). This prefix can be dynamically changed by using the | |
; '-p' argument from the command line. | |
;;;;;;;;;;;;;;;;;; | |
; Global Options ; | |
;;;;;;;;;;;;;;;;;; | |
[global] | |
; Pid file | |
; Note: the default prefix is /var | |
; Default Value: none | |
pid = /run/php/php7.3-fpm.pid | |
; Error log file | |
; If it's set to "syslog", log is sent to syslogd instead of being written | |
; in a local file. | |
; Note: the default prefix is /var | |
; Default Value: log/php-fpm.log | |
error_log = /proc/self/fd/2 | |
; syslog_facility is used to specify what type of program is logging the | |
; message. This lets syslogd specify that messages from different facilities | |
; will be handled differently. | |
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON) | |
; Default Value: daemon | |
;syslog.facility = daemon | |
; syslog_ident is prepended to every message. If you have multiple FPM | |
; instances running on the same server, you can change the default value | |
; which must suit common needs. | |
; Default Value: php-fpm | |
;syslog.ident = php-fpm | |
; Log level | |
; Possible Values: alert, error, warning, notice, debug | |
; Default Value: notice | |
;log_level = notice | |
; If this number of child processes exit with SIGSEGV or SIGBUS within the time | |
; interval set by emergency_restart_interval then FPM will restart. A value | |
; of '0' means 'Off'. | |
; Default Value: 0 | |
;emergency_restart_threshold = 0 | |
; Interval of time used by emergency_restart_interval to determine when | |
; a graceful restart will be initiated. This can be useful to work around | |
; accidental corruptions in an accelerator's shared memory. | |
; Available Units: s(econds), m(inutes), h(ours), or d(ays) | |
; Default Unit: seconds | |
; Default Value: 0 | |
;emergency_restart_interval = 0 | |
; Time limit for child processes to wait for a reaction on signals from master. | |
; Available units: s(econds), m(inutes), h(ours), or d(ays) | |
; Default Unit: seconds | |
; Default Value: 0 | |
;process_control_timeout = 0 | |
; The maximum number of processes FPM will fork. This has been design to control | |
; the global number of processes when using dynamic PM within a lot of pools. | |
; Use it with caution. | |
; Note: A value of 0 indicates no limit | |
; Default Value: 0 | |
; process.max = 128 | |
; Specify the nice(2) priority to apply to the master process (only if set) | |
; The value can vary from -19 (highest priority) to 20 (lower priority) | |
; Note: - It will only work if the FPM master process is launched as root | |
; - The pool process will inherit the master process priority | |
; unless it specified otherwise | |
; Default Value: no set | |
; process.priority = -19 | |
; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging. | |
; Default Value: yes | |
daemonize = no | |
; Set open file descriptor rlimit for the master process. | |
; Default Value: system defined value | |
;rlimit_files = 1024 | |
; Set max core size rlimit for the master process. | |
; Possible Values: 'unlimited' or an integer greater or equal to 0 | |
; Default Value: system defined value | |
;rlimit_core = 0 | |
; Specify the event mechanism FPM will use. The following is available: | |
; - select (any POSIX os) | |
; - poll (any POSIX os) | |
; - epoll (linux >= 2.5.44) | |
; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0) | |
; - /dev/poll (Solaris >= 7) | |
; - port (Solaris >= 10) | |
; Default Value: not set (auto detection) | |
;events.mechanism = epoll | |
; When FPM is build with systemd integration, specify the interval, | |
; in second, between health report notification to systemd. | |
; Set to 0 to disable. | |
; Available Units: s(econds), m(inutes), h(ours) | |
; Default Unit: seconds | |
; Default value: 10 | |
;systemd_interval = 10 | |
;;;;;;;;;;;;;;;;;;;; | |
; Pool Definitions ; | |
;;;;;;;;;;;;;;;;;;;; | |
; Multiple pools of child processes may be started with different listening | |
; ports and different management options. The name of the pool will be | |
; used in logs and stats. There is no limitation on the number of pools which | |
; FPM can handle. Your system will tell you anyway :) | |
; Include one or more files. If glob(3) exists, it is used to include a bunch of | |
; files from a glob(3) pattern. This directive can be used everywhere in the | |
; file. | |
; Relative path can also be used. They will be prefixed by: | |
; - the global prefix if it's been set (-p argument) | |
; - /usr otherwise | |
include=/etc/php/7.3/fpm/pool.d/*.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ------------------------------------------------------------------------------------------ | |
; This sets supervisord to run in the foreground (required by Docker to run it). | |
; | |
; We then run Nginx and PHP-FPM, making sure to set log files to be coming from stderr/stdout | |
; (and ensuring Supervisord doesn't attempt to "rotate" log files for these streams of text). | |
; ------------------------------------------------------------------------------------------ | |
[supervisord] | |
nodaemon=true | |
[program:nginx] | |
command=nginx | |
stdout_logfile=/dev/stdout | |
stdout_logfile_maxbytes=0 | |
stderr_logfile=/dev/stderr | |
stderr_logfile_maxbytes=0 | |
[program:php-fpm] | |
command=php-fpm7.3 | |
stdout_logfile=/dev/stdout | |
stdout_logfile_maxbytes=0 | |
stderr_logfile=/dev/stderr | |
stderr_logfile_maxbytes=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# ############################################################################# | |
# NAME | |
# laravel-new - create a new laravel installation | |
# | |
# SYNOPSIS | |
# ./laravel-new <NAME> | |
# | |
# DESCRIPTION | |
# This is a helper script that creates a fresh Laravel installation | |
# in the current directory, using an existing docker composer image | |
# | |
# NOTES | |
# Docker must been preinstalled | |
# ############################################################################# | |
# | |
if [[ $# -gt 0 ]];then | |
docker container run \ | |
-w /app \ | |
--mount type=bind,source="$(pwd)",target=/app \ | |
--user $(id -u):$(id -g) \ | |
--rm \ | |
composer:1.8.0 \ | |
composer create-project --prefer-dist laravel/laravel "$1" | |
else | |
echo -e "\n\033[031m ✗ Please provide a name for the new laravel application \033[0m\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment