├── .gitignore
├── LICENSE
├── docker-compose.yml
├── docker
├── Dockerfile
├── nginx
│ ├── error.html
│ └── sites.conf
├── php
│ ├── extra-php-fpm.conf
│ └── extra-php.ini
└── supervisord
│ └── supervisord.conf
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | app
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Urnau Tecnologias e outros
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3.9"
2 | services:
3 | web:
4 | image: urnau/app1-php:prod-nginx-php-fpm8.2.3
5 | container_name: web
6 | build:
7 | context: .
8 | dockerfile: ./docker/Dockerfile
9 | args:
10 | PHP_VERSION: '8.2.4-fpm-bullseye'
11 | # volumes:
12 | # - ./app:/var/www/app
13 | ports:
14 | - "80:80" #http
15 | - "443:443" #https
16 |
--------------------------------------------------------------------------------
/docker/Dockerfile:
--------------------------------------------------------------------------------
1 | ARG PHP_VERSION
2 | FROM php:${PHP_VERSION}
3 |
4 | ## Diretório da aplicação
5 | ARG APP_DIR=/var/www/app
6 |
7 | ## Versão da Lib do Redis para PHP
8 | ARG REDIS_LIB_VERSION=5.3.7
9 |
10 | ### apt-utils é um extensão de recursos do gerenciador de pacotes APT
11 | RUN apt-get update -y && apt-get install -y --no-install-recommends \
12 | apt-utils \
13 | supervisor
14 |
15 | # dependências recomendadas de desenvolvido para ambiente linux
16 | RUN apt-get update && apt-get install -y \
17 | zlib1g-dev \
18 | libzip-dev \
19 | unzip \
20 | libpng-dev \
21 | libpq-dev \
22 | libxml2-dev
23 |
24 | RUN docker-php-ext-install mysqli pdo pdo_mysql pdo_pgsql pgsql session xml
25 |
26 | # habilita instalação do Redis
27 | RUN pecl install redis-${REDIS_LIB_VERSION} \
28 | && docker-php-ext-enable redis
29 |
30 | RUN docker-php-ext-install zip iconv simplexml pcntl gd fileinfo
31 |
32 | # Composer
33 | RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
34 |
35 | COPY ./docker/supervisord/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
36 | COPY ./docker/supervisord/conf /etc/supervisord.d/
37 | ### Supervisor permite monitorar e controlar vários processos (LINUX)
38 | ### Bastante utilizado para manter processos em Daemon, ou seja, executando em segundo plano
39 |
40 | COPY ./docker/php/extra-php.ini "$PHP_INI_DIR/99_extra.ini"
41 | COPY ./docker/php/extra-php-fpm.conf /etc/php8/php-fpm.d/www.conf
42 |
43 | WORKDIR $APP_DIR
44 | RUN cd $APP_DIR
45 | RUN chown www-data:www-data $APP_DIR
46 |
47 | COPY --chown=www-data:www-data ./app .
48 | RUN rm -rf vendor
49 | RUN composer install --no-interaction
50 |
51 | RUN apt-get install nginx -y
52 | RUN rm -rf /etc/nginx/sites-enabled/* && rm -rf /etc/nginx/sites-available/*
53 | COPY ./docker/nginx/sites.conf /etc/nginx/sites-enabled/default.conf
54 | COPY ./docker/nginx/error.html /var/www/html/error.html
55 |
56 | RUN apt-get clean && rm -rf /var/lib/apt/lists/*
57 | # RUN apt update -y && apt install nano git -y
58 |
59 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
--------------------------------------------------------------------------------
/docker/nginx/error.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Página de Erro
8 |
9 |
10 |
11 |
12 | Erro ao carregar a página.
13 |
14 |
15 |
--------------------------------------------------------------------------------
/docker/nginx/sites.conf:
--------------------------------------------------------------------------------
1 | server {
2 | listen 80 default_server;
3 | listen [::]:80 default_server ipv6only=on;
4 |
5 | server_name localhost 127.0.0.1;
6 | root /var/www/app/public;
7 |
8 | add_header X-Frame-Options "SAMEORIGIN";
9 | add_header X-Content-Type-Options "nosniff";
10 | large_client_header_buffers 4 32k;
11 | client_max_body_size 100M;
12 |
13 | index index.php index.html;
14 |
15 | charset utf-8;
16 |
17 | location / {
18 | try_files $uri $uri/ /index.php?$query_string;
19 | }
20 |
21 | location = /favicon.ico { access_log off; log_not_found off; }
22 | location = /robots.txt { access_log off; log_not_found off; }
23 |
24 | error_page 500 503 /error.html;
25 | location = /error.html {
26 | root /var/www/html;
27 | internal;
28 | }
29 |
30 | location ~ \.php$ {
31 | try_files $uri /index.php =404;
32 | fastcgi_pass 127.0.0.1:9000;
33 | fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
34 | include fastcgi_params;
35 | fastcgi_index index.php;
36 | fastcgi_buffers 16 16k;
37 | fastcgi_buffer_size 32k;
38 | fastcgi_read_timeout 120;#2min
39 | }
40 |
41 | location ~ /\.(?!well-known).* {
42 | deny all;
43 | }
44 | }
--------------------------------------------------------------------------------
/docker/php/extra-php-fpm.conf:
--------------------------------------------------------------------------------
1 | #[wordpress_site]: The name of the pool and must be unique across all pool names.
2 | [extra_config_app]
3 |
4 | # user and group: The user and group under which the pool will run.
5 | # user=www-data
6 | # group=www-data
7 |
8 | # listen: The name of the socket file for this pool.
9 | listen = 127.0.0.1:9000
10 |
11 | # listen.owner and listen.group: Must match to the user and group on which NGINX is running. In our case it is www-data.
12 | # listen.owner=www-data
13 | # listen.group=www-data
14 | # listen.mode=0660
15 |
16 | # pm: The process manager settings and the value is Dynamic means the number of child processes are set dynamically based on the following directives.
17 | pm = dynamic
18 |
19 | # pm.max_children: The maximum number of children that can be alive at the same time.
20 | pm.max_children = 20
21 | # pm.start_servers: The number of children created on startup.
22 | pm.start_servers = 2
23 | # pm.min_spare_servers: The minimum number of children in ‘idle’ state (waiting to process). If the number of idle processes is less than this number then some children will be created.
24 | pm.min_spare_servers = 5
25 | #pm.max_spare_servers: The maximum number of children in idle state (waiting to process). If the number of idle processes is greater than this number then some children will be killed.
26 | pm.max_spare_servers = 20
27 | #pm.process_idle_timeout: The desired maximum number of idle server processes. Used only when pm value is set to dynamic. Apart from above settings, it is also possible to pass few system environmental variable to php-fpm service using something like env['PHP_FOO'] = $bar. For example, adding the following options in the above configuration file will set the hostname and temporary folder location to the PHP environment.
28 | pm.process_idle_timeout = 10s
29 |
30 |
31 |
--------------------------------------------------------------------------------
/docker/php/extra-php.ini:
--------------------------------------------------------------------------------
1 | [PHP]
2 | post_max_size = 10M
3 | upload_max_filesize = 10M
4 | max_execution_time=120
5 | max_input_time=-1
6 | memory_limit=256M
7 | max_input_vars=10000
8 | variables_order = EGPCS
--------------------------------------------------------------------------------
/docker/supervisord/supervisord.conf:
--------------------------------------------------------------------------------
1 | [unix_http_server]
2 | file=/var/run/supervisor.sock
3 | chmod=0770
4 | chown=nobody:nogroup
5 | # username = mysupervisord
6 | # password = mysupervisordpass
7 |
8 | [supervisord]
9 | nodaemon=true
10 | user=root
11 |
12 | [program:nginx]
13 | command = nginx -c /etc/nginx/nginx.conf -g 'daemon off;'
14 | user = root
15 | autostart = true
16 |
17 | [program:php-fpm]
18 | command=docker-php-entrypoint php-fpm
19 | user=root
20 | autostart=true
21 | nodaemon=true
22 | autorestart=true
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Sobre
2 |
3 | Neste repositório vamos fazer a criação de uma imagem Docker que embora possa ser utilizada em produção, ainda merece ser aperfeiçoada para permitir realmente o escalonamento da aplicação.
4 |
5 | # Conteúdo da Imagem Docker
6 |
7 | - PHP, e diversas extensões e Libs do PHP, incluindo php-redis, pgsql, mysql, entre outras.
8 |
9 | - Nginx, como proxy reverso/servidor. Por fim de testes é que o Nginx está presente nesta imagem, em um momento de otimização está imagem deixará de ter o Nginx.
10 |
11 | - Supervisor, indispensal para executarmos a aplicação PHP e permitir por exemplo a execução de filas e jobs.
12 |
13 | - Composer, afinal de contas é preciso baixar as dependências mais atuais toda vez que fomos crontruir uma imagem Docker.
14 |
15 | # Vídeos Tutorial
16 |
17 | [Vídeo Sobre Criação do Dockerfile e do Docker Compose file](https://youtu.be/iDJjb2zYa4c)
18 |
19 | # Passo a Passo
20 |
21 | ## Certifique-se de estar com o Docker em execução.
22 |
23 | ```sh
24 | docker ps
25 | ```
26 |
27 | ## Certifique-se de ter o Docker Compose instalado.
28 |
29 | ```sh
30 | docker compose version
31 | ```
32 |
33 | ## Clone sua aplicação Laravel para a pasta 'app'. Caso a pasta app não existe, crie a pasta.
34 |
35 | A listagem de pastas do projeto deve ficar:
36 |
37 | ```
38 | app/
39 | docker/
40 | .gitignore
41 | docker-compose.yml
42 | readme.md
43 | ```
44 |
45 | ## Certifique-se que sua aplicação Laravel ficou em `./app` e que existe o seguinte caminho: `/app/public/index.php`
46 |
47 | ## Certifique-se que sua aplicação Laravel possuí um .env e que este .env está com a `APP_KEY=` definida com valor válido.
48 |
49 | ## Contruir a imagem Docker, execute:
50 |
51 | ```sh
52 | docker compose build
53 | ```
54 |
55 | ## Caso não queira utilizar o cache da imagem presente no seu ambiente Docker, então execute:
56 |
57 | ```sh
58 | docker compose build --no-cache
59 | ```
60 |
61 | ## Para subir a aplicação, execute:
62 |
63 | ```sh
64 | docker compose up
65 | ```
66 |
67 | - Para rodar o ambiente sem precisar manter o terminar aberto, execute:
68 |
69 | ```sh
70 | docker compose up -d
71 | ```
72 |
73 | ## Para derrubar a aplicação, execute:
74 |
75 | ```sh
76 | docker compose down
77 | ```
78 |
79 | ## Para entrar dentro do Container da Aplicação, execute:
80 |
81 | ```sh
82 | docker exec -it web bash
83 | ```
84 |
85 | # Solução de Problemas
86 |
87 | ## Problema de permissão
88 |
89 | - Quando for criado novos arquivos, ou quando for a primeira inicialização do container com a aplicação, pode então haver um erro de permissão de acesso as pastas, neste caso, entre dentro do container da aplicação e execeute.
90 |
91 | ```sh
92 | cd /var/www && \
93 | chown -R www-data:www-data * && \
94 | chmod -R o+w app
95 | ```
96 |
--------------------------------------------------------------------------------