Laravel Dockerfile > 1GB

402 views Asked by At

I'm building a Docker Image for my Laravel application but it's getting bigger than 1GB. This is the Dockerfile:

#
# PHP Dependencies
#
FROM composer as vendor

COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
    --ignore-platform-reqs \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist

#
# Application
#
FROM php:fpm-alpine
RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl \
        libtool \
        libxml2-dev \
    && apk add --no-cache \
        curl \
        git \
        mysql-client \
    && pecl install redis \
    && docker-php-ext-install \
        pdo \
        pdo_mysql \
        tokenizer \
        bcmath \
        opcache \
        xml \
    && apk del -f .build-deps \
    && docker-php-ext-enable \
       pdo_mysql \
       redis

WORKDIR /var/www/html

COPY . /var/www/html
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY .env.staging /var/www/html/.env

RUN chown -R root:www-data . 

EXPOSE 9000

CMD ["php-fpm"]

I use Vue as front-end and Redis as caching provider.

I am used to images around the 200-300 MBs but this is ridiculous.

What can I do to reduce the image size?

Thanks in advance.

1

There are 1 answers

1
Yasen On

Since docker filesystem is OverlayFS, you need a special tool to dive into FS layers.

There are many tools to explore images. For your case I'd suggest wagoodman/dive: A tool for exploring each layer in a docker image

docker run --rm -it \ 
  -v /var/run/docker.sock:/var/run/docker.sock \
  wagoodman/dive:latest \
  <image_name|image_id>

A tool for exploring a docker image, layer contents, and discovering ways to shrink your Docker image size. Image

As you can see on the screenshot - Layers table has Size column.

So, you can find - which layer of your docker image has the greatest contribution to the volume of the final image.