Score:0

How to properly setup composer within PHP Apache Docker Container

in flag

Content of my Dockerfile:

FROM php:8.1-apache

WORKDIR /var/www/html/

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && a2enmod rewrite \
    && docker-php-ext-install zip

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY composer.json composer.json
COPY composer.lock composer.lock

RUN groupadd -r user && useradd -r -g user user
USER user
RUN composer install --no-dev

COPY . .

EXPOSE 80

The composer installation fails at the point where it's telling me that the libzip package is needed. I can however not find that package, no matter if via pecl, via apt-get; as most of the forum posts refer to. How can I Install libzip to finally get things running?

A few notes on my Dockerfile lines; feel free to correct me if I'm wrong in the setup; it's the first time I'm doing this:

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY composer.json composer.json
COPY composer.lock composer.lock

As I understood it, the official docs tell you to use this multi-stage approach to get composer running in your PHP Docker container in an optimal way. If I understood it correctly, doing it in this way allows you to share your image without your colleagues needing to install composer / download the packages locally; everything will then be done within the container. Which is exactly what I'm looking for.

RUN groupadd -r user && useradd -r -g user user
USER user
RUN composer install --no-dev

I first tried to simply run the last line composer install --no-dev, which actually won't work, as the container won't let you execute root commands; used by default by composer. Hence the reason for this workaround (changing the user is also what the official docs recommend; according to the link above).

&& docker-php-ext-install zip

When finally able to run the composer install command within the container, it told me that the zip extension is needed; which is why I've added the docker-php-ext-install zip command. Now I'm stuck due to the initially mentioned libzip issue.

If you see a better way to setup composer; I'm happy to hear about a better way.

Score:1
in flag

docker-php-ext-enable is looking for the libzip header files. What you need is the package libzip-dev.

RUN pecl install xdebug \
    && apt update \
    && apt install libzip-dev -y \
    && docker-php-ext-enable xdebug \
    && a2enmod rewrite \
    && docker-php-ext-install zip \
    && rm -rf /var/lib/apt/lists/*
DevelJoe avatar
in flag
Great, cheers, that works! The only problem now is that there's no `vendor` folder in my container root??
in flag
That would be a new question. The relevant output from the build would probably be helpful.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.