Skip to content

Commit

Permalink
Merge pull request #4 from marcosfreitas/develop
Browse files Browse the repository at this point in the history
Refactoring to Alpine
  • Loading branch information
marcosfreitas authored Nov 2, 2020
2 parents 5aa7d26 + 165b183 commit 7fee566
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 497 deletions.
52 changes: 52 additions & 0 deletions .docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @version 4.0.0
# Configures NGINX .

FROM nginx:1.19-alpine
LABEL maintainer="marcosfreitas@c4network.com.br"

# @todo change user to not run as root
#RUN apk add --no-cache shadow &&\
# usermod -u 1000 nginx

#USER nginx

EXPOSE 80
# @todo not prepared to work with SSL connections yet
EXPOSE 443

# @bug misconfiguration, should be tested
#RUN cd / &&\
# wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh &&\
# chmod +x wait-for-it.sh

RUN apk update && apk add --no-cache openssl shadow bash wait4ports &&\
rm /etc/nginx/nginx.conf &&\
rm /etc/nginx/conf.d/default.conf

# From the context of this Dockerfile
ADD /nginx/nginx.conf /etc/nginx/nginx.conf

#ENV DOCKERIZE_VERSION v0.6.1
#RUN wget https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \
# && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \
# && rm dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz

# @bug problem with folder's permissions
RUN mkdir -p /etc/nginx/sites-enabled &&\
mkdir -p /etc/nginx/sites-available &&\
chmod 751 -R /etc/nginx;

ADD /nginx/vhost-app.conf /etc/nginx/sites-available/app

RUN ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app

RUN ls -l /etc/nginx/conf.d;
RUN ls -l /etc/nginx/;

# @bug problem with relative paths
#RUN rm -rf /var/www/html
#VOLUME /../../www /var/www/html
#VOLUME /../../.docker/nginx/templates /etc/nginx/templates
#RUN ls /etc/nginx/templates/snippets;

CMD ["nginx", "-g", "daemon off;"]
7 changes: 3 additions & 4 deletions src/server/nginx/nginx.conf → .docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# worker_connection can be multiplied by worker_processes to improve performance
# Check out Nginx' documentation for more details.

user www-data;
user nginx;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
Expand Down Expand Up @@ -40,7 +40,7 @@ http {
tcp_nodelay on;
types_hash_max_size 2048;

include snippets/security-http.conf;
include /etc/nginx/conf.d/snippets/security-http.conf;

# @todo server_names_hash_bucket_size 64;
# @todo server_name_in_redirect off;
Expand All @@ -63,6 +63,5 @@ http {
# Virtual Host Configs
##

#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/sites-enabled/*;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ location / {

# pass PHP scripts to FastCGI server

include snippets/php.conf;
include conf.d/snippets/php.conf;

# if the laravel project is located into a subdirectory

Expand All @@ -29,7 +29,7 @@ include snippets/php.conf;
# try_files $uri $uri/ @sub_directory;

# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# include conf.d/snippets/fastcgi-php.conf;
# fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# }
# }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ if ($query_string != "") {
set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}

# "punching a hole" through the cache
# To support Pragma: no-cache sent by clients and force touch the resource without cache
# and Acessible as http://www.example.com/?nocache=true
Expand Down
24 changes: 24 additions & 0 deletions .docker/nginx/templates/snippets/http-server.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @info default http:80 server

server {
listen ${NGINX_PORT} default_server;
listen [::]:${NGINX_PORT} default_server;

root ${NGINX_ROOT};

server_name ${NGINX_HOST};

# to SSL permanent redirect for all request on port 80
#server_name _;
#return 301 https://$host$request_uri;

# load general security configurations
include conf.d/snippets/security-locations.conf;

# Performance configuration
include conf.d/snippets/cache-directives.conf;
include conf.d/snippets/performance.conf;

# base configurations of virtual host
include conf.d/snippets/base-vhosts.conf;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# cache-directives.conf already have bypasses

location ~ \.php$ {
#include snippets/fastcgi-php.conf;
#include conf.d/snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include snippets/fastcgi-extra-php-params.conf;
include conf.d/snippets/fastcgi-extra-php-params.conf;

# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_pass ${PHP_HOST}:${PHP_PORT};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# the following configuration snippet implements a 1‑second caching period for responses with a 200 OK status code.
fastcgi_cache_path /tmp/cache keys_zone=APPLICATION:10m levels=1:2 inactive=60m max_size=500m use_temp_path=off;

include snippets/http-server.conf;
include conf.d/snippets/http-server.conf;
31 changes: 31 additions & 0 deletions .docker/php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM php:7.4-fpm-alpine
LABEL maintainer="marcosfreitas@c4network.com.br"

ARG APP_ENVIROMENT

EXPOSE 9000

# Added script to easy install PHP extensions

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/

# @info fix permissions
# Use the default production configuration
RUN apk update && apk add --no-cache shadow bash composer &&\
usermod -u 1000 www-data &&\
mv "$PHP_INI_DIR/php.ini-$APP_ENVIROMENT" "$PHP_INI_DIR/php.ini" &&\
install-php-extensions mysqli pdo_mysql bcmath gd &&\
php-fpm -m

COPY /src/scripts/ /usr/share/.docker/src/scripts

RUN chmod +x /usr/share/.docker/src/scripts/*.sh &&\
mkdir -p /var/www/html &&\
bash /usr/share/.docker/src/scripts/set-right-permissions-by-path.sh /var/www/ &&\
bash /usr/share/.docker/src/scripts/set-right-permissions-by-path.sh /var/www/html

WORKDIR /var/www/html

USER www-data

ENTRYPOINT [ "/usr/share/.docker/src/scripts/laravel-initial-configurations.sh" ]
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/env bash

echo "-- Running composer install";

cd /var/www/html;

composer install;
if [[ -f 'composer.json' ]]; then
echo "-- Running composer install";
composer install;
fi;

CONTAINER_ALREADY_STARTED="CONTAINER_ALREADY_RAN_ONCE"

if [ ! -e $CONTAINER_ALREADY_STARTED ]; then
if [[ ! -e $CONTAINER_ALREADY_STARTED && -f 'artisan' ]]; then
touch $CONTAINER_ALREADY_STARTED

echo "-- Container first time for its first time"
echo "-- Running this container first its very first time"

chmod a+rw -R /var/www/;

Expand All @@ -32,9 +33,11 @@ if [ ! -e $CONTAINER_ALREADY_STARTED ]; then
chmod a+rw -R /var/www/;

else
echo "-- Container already run. No need to be reconfigured"
echo "-- Container already run, no need to be reconfigured or Laravel is not installed."
fi

service nginx start && service php7.2-fpm start && /bin/bash
echo "-- Services running";

php-fpm;

echo "-- Services running";
service php-fpm status;
21 changes: 21 additions & 0 deletions .docker/src/scripts/set-right-permissions-by-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Run as:
# bash this-script.sh /var/www and again passing your project folder /var/www/project-path

FILE_PATH=$1;

if [[ ! -d "$FILE_PATH" ]]; then
echo 'Path argument not defined';
exit;
fi;

chmod 775 ${FILE_PATH} -R && \
chmod 2775 ${FILE_PATH} -R && \
chown -R www-data:www-data ${FILE_PATH} -R;

if [[ ! -z $(ls -A .) ]]; then
cd ${FILE_PATH} && find * -type d -exec chmod -R 775 {} \; && find * -type f -exec chmod -R 664 {} \;
else
echo 'Path is empty, not running commands for children.';
fi;
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPOSE_PROJECT_NAME=SKINNY_HOSTS
APP_ENVIROMENT=development
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__OLD
!/.docker
www
34 changes: 0 additions & 34 deletions Dockerfile

This file was deleted.

33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Laravel Host em docker - para testes
Imagem pré-configurada com nginx e php-fpm 7.2 para efeito de testes. Não utilize em produção.
# Imagens Alpine com NGINX, PHP-FPM e MySQL para hospedagem Laravel em Docker (Em desenvolvimento)

**Os arquivos Laravel devem ser instalados previamente na pasta www.**

[![Docker Build Status](https://img.shields.io/docker/automated/marcosfreitas/docker-laravel-host?style=for-the-badge)](https://hub.docker.com/r/marcosfreitas/docker-laravel-host/)
Este projeto está equipado com docker-compose, ao clonar você pode executar desta forma:

`docker-compose up -d`

A imagem construída do projeto Laravel foi publicada no Docker HUB:

https://hub.docker.com/r/marcosfreitas/skinny-hosts/

[![Docker Build Status](https://img.shields.io/docker/automated/marcosfreitas/skinny-hosts?style=for-the-badge)](https://hub.docker.com/r/marcosfreitas/skinny-hosts/)

# Detalhes dos Containeres

### NGINX
O serviço de NGINX possui configurações customizadas a partir dos templates em **.docker/nginx**.
Ele depende do serviço de php e se comunica pelo host e porta passados nas variáveis de ambiente.

### PHP-FPM
O serviço de PHP-FPM não possui configurações customizadas, mas a construção do container prepara as permissões corretas de arquivos e a instalação do Laravel de acordo com os scripts Bash nas pasta **.docker/src/scripts**.

### MySQL
O MySQL possui a versão 8+ com implementação de fallback de autenticação para versão 5.7.


## Detalhes

Na raiz do repositório há o arquivo .env que define o nome do projeto que reflete nas configurações do Laravel, prefixo de containeres e nome de Banco de dados, além do ambiente (development ou production) para o php-fpm.

O arquivo docker-compose.yml implementa outras variáveis de ambiente utilizadas pelo dockerize.
Loading

0 comments on commit 7fee566

Please sign in to comment.