refactor docker setup

This commit is contained in:
Mohit Panjwani
2020-05-27 14:18:29 +05:30
parent dc37f565c4
commit 6278417423
7 changed files with 108 additions and 138 deletions

View File

@ -1,53 +1,40 @@
##### STAGE 1 #####
FROM php:7.4-fpm
FROM composer as composer
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Copy composer files from project root into composer container's working dir
COPY composer.* /app/
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
libzip-dev
# Copy database directory for autoloader optimization
COPY database /app/database
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Run composer to build dependencies in vendor folder
RUN composer install --no-scripts --no-suggest --no-interaction --prefer-dist --optimize-autoloader
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl bcmath gd
# Copy everything from project root into composer container's working dir
COPY . /app
RUN composer dump-autoload --optimize --classmap-authoritative
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
##### STAGE 2 #####
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
FROM php:7.3.12-fpm-alpine
# Set working directory
WORKDIR /var/www
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
USER $user
RUN apk add --no-cache libpng-dev libxml2-dev oniguruma-dev libzip-dev gnu-libiconv && \
docker-php-ext-install bcmath ctype json gd mbstring pdo pdo_mysql tokenizer xml zip
# COPY ./docker-compose/setup.sh /tmp
# RUN ln -s /usr/local/bin/setup.sh
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
# Set container's working dir
WORKDIR /app
# Copy everything from project root into php container's working dir
COPY . /app
# Copy vendor folder from composer container into php container
COPY --from=composer /app/vendor /app/vendor
RUN touch database/database.sqlite && \
cp .env.example .env && \
php artisan config:cache && \
php artisan passport:keys && \
php artisan key:generate && \
chown -R www-data:www-data . && \
chmod -R 755 . && \
chmod -R 775 storage/framework/ && \
chmod -R 775 storage/logs/ && \
chmod -R 775 bootstrap/cache/
EXPOSE 9000
CMD ["php-fpm", "--nodaemonize"]
# ENTRYPOINT ["/tmp/setup.sh"]