简体   繁体   中英

Composer Install (own Container) with Docker missing PHP Extensions

I am currently learning about Docker, and using it for 2 weeks. Now i have a very simple task, installing PHP Libraries via Composer. That is usually, when working without Docker:

composer install

Now since i am using Docker, i found there is a Docker Container, that is holding composer for me:

docker run --rm -v $(pwd):/app composer/composer install

This is working pretty good, but there is some libraries out there, that require specific php libraries to be installed, like bcmath, so i add this to my Dockerfile

FROM php:7.0-apache
RUN docker-php-ext-install bcmath <-- added this line 
COPY . /var/www/html
WORKDIR /var/www/html
EXPOSE 80

When i rebuild my container, this code returns true

var_dump(extension_loaded('bcmath'))

Hooray! BCMath is installed correctly, but composer does not recognize it, because the library is not installed in the composer container!

Now i could ignore that by using

docker run --rm -v $(pwd):/app composer/composer install --ignore-platform-reqs

but this is, in my opinion, a dirty workound, and composer can not validate my platform. Is there any clean solution, besides downloading composer in my Dockerfile and not reusing an existing container?

You need PHP + PHP Extensions + Composer in the same(!) container = DevContainer.

Simply install Composer using the command provided here .

You may use platform settings to mimic your PHP container configuration. This will work similar to --ignore-platform-reqs switch (it will use PHP and extensions configured in composer.json instead of real info from current PHP installation), but it gives you more granular control. Instead of "ignore all platform requirements checks" you may say "I really have bcmath installed, trust me". All other requirements will be checked, so you still be warned if new requirement will pop-up.

"config": {
    "platform": {
        "php": "7.1",
        "ext-bcmath": "*"
    }
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM