10

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?

CBergau
  • 636
  • 2
  • 10
  • 26

2 Answers2

3

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": "*"
    }
},
rob006
  • 21,383
  • 5
  • 53
  • 74
-1

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

Simply install Composer using the command provided here.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Please add some further explanation. Wouldn't this defeat the whole purpose of having a seperated Composer container? What's the reason they are published on Docker hub if they are useless? – Nico Haase May 24 '20 at 09:55
  • Composer Docker Images are not useless. People have different use cases. They provide a quick way of running Composer standalone. But they are not useful as a base image. The optimal solution is to create your own image using the specific PHP version and PHP extensions you need to get your app up and running and this includes Composer. Small, efficient and fast to install. A multi staging with different PHP versions as base plus PHP extension layer plus Composer layer will take more space + slower to install. It's also the suggested way by the Composer Devs, https://hub.docker.com/_/composer – Jens A. Koch May 25 '20 at 01:40
  • Please add all explanation to your answer by editing it – Nico Haase May 25 '20 at 05:16