bitpressio / docker-for-php-developers-errata

Reported errata for https://bitpress.io/docker-for-php-developers/
13 stars 0 forks source link

14 - PHP Composer (Part 3) - Caching Components ~02:40 #21

Closed avrahamm closed 4 years ago

avrahamm commented 4 years ago

Hi Paul!

I am on Windows 10, till chapter 14 it was OK. Yet in chapter 14, I followed exactly till ~02:40 docker build -t composer:latest -f docker/Dockerfile . got

failed with /bin/sh: 1: composer.json: not found
Generating optimized autoload files
/bin/sh: 1: composer.json: not found
The command '/bin/sh -c mkdir -p database/seeds/     mkdir -p database/factories/     && composer install         --no-interaction         --no-plugins         --no-scripts         --prefer-dist         && composer.json composer.lock auth.json vendor/ database/' returned a non-zero code: 127

How to fix it?

Thanks, Avraham

paulredmond commented 4 years ago

@avrahamm seems to work for me. Here's the Dockerfile from that lesson:

FROM php:7.2-apache-stretch

COPY docker/composer-installer.sh /usr/local/bin/composer-installer

RUN apt-get -yqq update \
    && apt-get -yqq install --no-install-recommends zip unzip \
    && chmod +x /usr/local/bin/composer-installer \
    && composer-installer \
    && mv composer.phar /usr/local/bin/composer \
    && chmod +x /usr/local/bin/composer \
    && composer --version

WORKDIR /tmp
COPY composer.json composer.lock auth.json /tmp/
RUN mkdir -p database/seeds/ database/factories/ \
    && composer install \
        --no-interaction \
        --no-plugins \
        --no-scripts \
        --prefer-dist \
    && rm -rf composer.json composer.lock auth.json vendor/ database/

WORKDIR /var/www/html

COPY . /var/www/html

RUN composer install \
        --no-interaction \
        --no-plugins \
        --no-scripts \
        --prefer-dist \
    && composer clear-cache
avrahamm commented 4 years ago

@paulredmond
It works fine, I had a typo. Thanks! Avraham

paulredmond commented 4 years ago

@avrahamm I'm glad you got it working!

avrahamm commented 4 years ago

Hi Paul, I try to explore by ls -la ~/.composer/cache

root@c52e39dd65cb:/var/www/html# ls -la ~/.composer total 24 drwxr-xr-x 1 root root 4096 Jan 7 19:25 . drwx------ 1 root root 4096 Jan 7 19:25 .. -rw-r--r-- 1 root root 13 Jan 7 19:25 .htaccess drwxr-xr-x 1 root root 4096 Jan 7 19:29 cache -rw-r--r-- 1 root root 799 Jan 7 19:25 keys.dev.pub -rw-r--r-- 1 root root 799 Jan 7 19:25 keys.tags.pub

root@c52e39dd65cb:/var/www/html# ls -la ~/.composer/cache total 8 drwxr-xr-x 1 root root 4096 Jan 7 19:29 . drwxr-xr-x 1 root root 4096 Jan 7 19:25 .. root@c52e39dd65cb:/var/www/html# ls -la ~/.composer/cache/ total 8 drwxr-xr-x 1 root root 4096 Jan 7 19:29 . drwxr-xr-x 1 root root 4096 Jan 7 19:25 .. root@c52e39dd65cb:/var/www/html#

Repetitive docker build -t composer:latest -f docker/Dockerfile . run faster, so I suppose it is cached somewhere, yet couldn't find files directory. Any idea? Dockerfile is attached. Thanks

Dockerfile.txt

paulredmond commented 4 years ago

@avrahamm ah, ya, that is confusing. I'll try to explain:

The first time you run composer install:

RUN mkdir -p database/seeds/ \
    mkdir -p database/factories/ \
    && composer install \
    # ...

Caches the dependencies, so when you run composer install again, the dependencies are there. If you run the following in the last RUN you should see them:

RUN ls -la ~/.composer/cache \
    && composer install \
        --no-interaction \
        --no-plugins \
        --no-scripts \
        --prefer-dist \
    && composer clear-cache

By clearing them it only removes them from the final container image, but doesn't reduce the image size. So running composer clear-cache at the end is optional. If you don't run that you will see the files in the final image.