phplinuxdockermakefilealpine-linux

Compiling php on development machine and run it in Alpine failed


I built php7 with the command below:

./configure --prefix=/root/testphp --with-openssl --enable-zip --enable-mbstring --with-pdo-mysql --enable-static
make
make install

And then I tried to run it on my development machine; it worked:

[root@guningvm ~]# /root/testphp/bin/php -v
PHP 7.3.4 (cli) (built: Apr  8 2019 11:25:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies

But when I mount the dir /root/testphp on an Alpine docker container:

docker run -itd --name "testforphp" -v /root/testphp/:/usr/local/php/ alpine:3.7

And run the php command in the container with /usr/local/php/bin/php -v, I get:

sh: /usr/local/php/bin/php: not found

Could anyone tell me why it didn't work? I've spent a lot of time on this problem to no avail.


Solution

  • It's not entirely clear, but it seems you are building PHP on your local Linux machine (say, Ubuntu), and trying to run it on an Alpine container.

    I'm also assuming you have good reasons for building PHP from source - otherwise, you could use the available PHP builds for Alpine.

    Building on non-Alpine Linux and running the resulting executable on Alpine usually won't work, since Alpine Linux uses a special libc (Standard C library) implementation called musl libc. musl is not compatible with the GNU C Library - glibc, used by most other Linux distros, so for running native software on Alpine Linux, it must be built against that same, musl libc library.

    The /usr/local/php/bin/php: not found error message is quite confusing, and assuming the file is present is likely to result from a dynamic linking failure. You could verify that using ldd /usr/local/php/bin/php.

    Therefore, you'll have to link PHP against the musl C library for running on Alpine, and most preferably, on an Alpine machine.

    I can think of the following options:

    1. Build PHP on an Alpine container. I'm not sure whether the main development PHP branch supports this (for example, OpenJDK Java Alpine support is developed under seperate projects, named IcedTea and Portola), but it is well worth a try. I believe it is possible, since PHP builds are available for Alpine - try following their build recipe, if you encounter difficulties. This is the best option, in my opinion.

    2. Keep building PHP on your non-Alpine Linux machine, but link it against the musl libc binary (usually available through packages), instead of glibc. This may be more difficult to achieve, and more prone to issues, so less recommended.

    3. Run your built PHP on a glibc-enabled Alpine container. This is a fairly simple procedure. However, this option is probably not a viable one, since your PHP would still be non compatible to vanilla Alpine, and can't be used by PHP Alpine users out-of-the-box. Plus, adding glibc to the container has several MB overhead, which conflicts with your goal for minimal size.