1

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.

Yasen
  • 4,241
  • 1
  • 16
  • 25
Dirk
  • 3,095
  • 4
  • 19
  • 37
  • It's hard to say without knowing exactly what you're copying into that container and what dependencies you have in your composer.json files, but have you considered using [Volumes](https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag)? Are you using this image strictly for Development purposes? Or do you plan on deploying it somewhere like Kubernetes or an ECS service? – maiorano84 Apr 12 '20 at 21:02
  • 1
    `docker history` will give you some hints as to which specific Dockerfile step is taking up space; you should also be able to run commands like `du` (for example `docker run --rm yourimage du -k /usr`) to see where space is going. – David Maze Apr 12 '20 at 22:28
  • What have you tried to find the cause for that size? How **exactly** did you measure the size? – Nico Haase Apr 13 '20 at 10:54

1 Answers1

1

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.

Yasen
  • 4,241
  • 1
  • 16
  • 25