Skip to content

Commit

Permalink
reescrita logica de inicio de serviços do dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
altendorfme committed Nov 22, 2024
1 parent 7cc0d1f commit d48862f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 91 deletions.
17 changes: 6 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ COPY app/ /app/
WORKDIR /app
RUN composer install --no-interaction --optimize-autoloader

COPY env.sh /usr/local/bin/env.sh
RUN chmod +x /usr/local/bin/env.sh
# Copy and set permissions for entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

COPY start.sh /usr/local/bin/start.sh
RUN chmod +x /usr/local/bin/start.sh

RUN mkdir -p /app/cache /app/logs \
&& chown -R www-data:www-data /app/cache /app/logs \
&& chmod -R 775 /app/cache /app/logs
RUN mkdir -p /app/cache /app/logs

# Set base permissions for /app
RUN chown -R www-data:www-data /app \
&& chmod -R 755 /app

VOLUME ["/app/cache", "/app/logs"]

EXPOSE 80

CMD ["/bin/bash", "-c", "/usr/local/bin/env.sh && /usr/local/bin/start.sh"]
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
10 changes: 4 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ services:
ports:
- "80:80"
volumes:
- marreta_cache:/app/cache
- marreta_logs:/app/logs
- ./app/cache:/app/cache
- ./app/logs:/app/logs
environment:
- SITE_NAME=${SITE_NAME:-}
- SITE_DESCRIPTION=${SITE_DESCRIPTION:-}
- SITE_URL=${SITE_URL:-}
- DNS_SERVERS=${DNS_SERVERS:-}
# Add user mapping to help with permissions
user: "${UID:-1000}:${GID:-1000}"
restart: unless-stopped

volumes:
marreta_cache:
marreta_logs:
118 changes: 118 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

###########################################
# Marreta Docker Entrypoint
#
# Este script inicializa o container do Marreta:
# - Configura variáveis de ambiente
# - Ajusta permissões dos diretórios
# - Inicia e verifica serviços (PHP-FPM e Nginx)
###########################################

# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Função para logs de sucesso
log_success() {
echo -e "${GREEN}[✓] $1${NC}"
}

# Função para logs de erro
log_error() {
echo -e "${RED}[✗] $1${NC}"
exit 1
}

# Função para logs de informação
log_info() {
echo -e "${YELLOW}[i] $1${NC}"
}

echo -e "\n${YELLOW}=== Iniciando Container Marreta ===${NC}\n"

# === Configuração de Variáveis de Ambiente ===
log_info "Configurando variáveis de ambiente..."

if [ -n "${SITE_NAME}" ]; then
echo "SITE_NAME=${SITE_NAME}" >> /app/.env
fi

if [ -n "${SITE_DESCRIPTION}" ]; then
echo "SITE_DESCRIPTION=${SITE_DESCRIPTION}" >> /app/.env
fi

if [ -n "${SITE_URL}" ]; then
echo "SITE_URL=${SITE_URL}" >> /app/.env
fi

if [ -n "${DNS_SERVERS}" ]; then
echo "DNS_SERVERS=${DNS_SERVERS}" >> /app/.env
fi

log_success "Variáveis de ambiente configuradas"

# === Ajuste de Permissões ===
log_info "Ajustando permissões dos diretórios..."

chown -R www-data:www-data /app/cache /app/logs
chmod -R 775 /app/cache /app/logs

log_success "Permissões ajustadas"

# === Funções de Verificação de Serviços ===
check_nginx() {
if ! pgrep nginx > /dev/null; then
log_error "Falha ao iniciar Nginx"
else
log_success "Nginx iniciado com sucesso"
fi
}

check_php_fpm() {
if ! pgrep php-fpm > /dev/null; then
log_error "Falha ao iniciar PHP-FPM"
else
log_success "PHP-FPM iniciado com sucesso"
fi
}

# === Inicialização dos Serviços ===
echo -e "\n${YELLOW}=== Iniciando serviços ===${NC}\n"

# Diretório PHP-FPM
if [ ! -d /var/run/php ]; then
log_info "Criando diretório PHP-FPM..."
mkdir -p /var/run/php
chown -R www-data:www-data /var/run/php
log_success "Diretório PHP-FPM criado"
fi

# Iniciando PHP-FPM
log_info "Iniciando PHP-FPM..."
php-fpm &
sleep 3
check_php_fpm

# Verificando configuração Nginx
log_info "Verificando configuração do Nginx..."
nginx -t
if [ $? -ne 0 ]; then
log_error "Configuração do Nginx inválida"
else
log_success "Configuração do Nginx válida"
fi

# Iniciando Nginx
log_info "Iniciando Nginx..."
nginx -g "daemon off;" &
sleep 3
check_nginx

echo -e "\n${GREEN}=== Container Marreta inicializado ===${NC}\n"

wait -n

exit $?
19 changes: 0 additions & 19 deletions env.sh

This file was deleted.

55 changes: 0 additions & 55 deletions start.sh

This file was deleted.

0 comments on commit d48862f

Please sign in to comment.