Create small Laravel docker image

Viewed 282

I have a project with separate frontend and backend (ReactJS + Laravel) and now I need the docker image of them also separate, the problem is that I can't create a docker image for Laravel smaller than 100mb.

My React directory is 700mb (with node_modules), but for production I do the "build" and just use the build on a Nginx docker image, generating a .tar of 50mb (the docker .tar is bigger than the normal, but 50mb is fine).

My Laravel directory has 240mb (with Vendor) and all the ways I used to create an image for production generate a .tar (final image) between 600mb and 1gb. How to make a "build" of laravel and use only it in Nginx like I did with Reactjs?

Important: If it's possible to use only one file (Dockerfile) as I do it will be PERFECT, I don't want to involve external configuration files or docker_composer, but if it's not possible I accept these alternatives.

My Nginx image using only reactjs build (52mb):


FROM node:16-alpine as build

ENV PATH /app/node_modules/.bin:$PATH

WORKDIR /app

COPY . /app

RUN yarn
RUN yarn add react-scripts@5.0 -g 

RUN yarn build

# -----------------------------------------------------------------------------
FROM fitiavana07/nginx-react

COPY --from=build /app/build /usr/share/nginx/html

EXPOSE 80

CMD nginx -g 'daemon off;'

My Laravel image (1gb):

FROM webdevops/php-nginx:8.0

# Get latest Composer
# COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN curl -sS https://getcomposer.org/installer | \ 
        php -- --install-dir=/usr/local/bin --filename=composer

RUN apt-get update
RUN apt-get -y install git libicu-dev libonig-dev libzip-dev \
        unzip locales libpng-dev libonig-dev libxml2-dev

RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*
RUN locale-gen en_US.UTF-8
RUN localedef -f UTF-8 -i en_US en_US.UTF-8
RUN mkdir /var/run/php-fpm

RUN docker-php-ext-install intl pdo_mysql zip bcmath mbstring \
        exif pcntl bcmath gd

ENV WEB_DOCUMENT_ROOT /app/public
ENV APP_ENV production

WORKDIR /app
COPY . .

RUN composer install --no-interaction --optimize-autoloader --no-dev
# Optimizing Configuration loading
RUN php artisan config:cache
# Optimizing Route loading
RUN php artisan route:cache
# Optimizing View loading
# RUN php artisan view:cache

RUN chown -R application:application .
0 Answers
Related