New php docker install exits code 0

My (ultimate) goal is to get an arduino to send data to a mariadb in a docker container via Apache w/php on a R-Pi 4.

  • I have MariaDB installed in a container and working.
  • I have HttpD (Apache) working–apparently because I can generate a simple web page.

I have Portainer installed and running in a container. I tried to install PHP in its own container using Portainer. The install finished successfully however, the container won’t stay running–exiting code 0.
The log:
Interactive shell
php > Interactive shell
php >

I am a wet-behind-the-ears Noob when it comes to Docker, but trying to understand. I have spent hours searching the web for (1) succinct instructions to install PHP in a container and (2) link it to Apache, however, I’ve come up short in matching the various responses.

Container Details

Image php:latest@sha256:ce207c49701153723ff877c7f4e1cb4646bd56176b03139612ebb0f107e1aba4
CMD php -a
ENTRYPOINT docker-php-entrypoint
ENV
GPG_KEYS AFD8691FDAEDF03BDF6E460563F15A9B715376CA 9D7F99A0CB8F05C8A6958D6256A97AF7600A39A6 0616E93D95AF471243E26761770426E17EBBB3DD
PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHP_ASC_URL https://www.php.net/distributions/php-8.4.5.tar.xz.asc
PHP_CFLAGS -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_CPPFLAGS -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_INI_DIR /usr/local/etc/php
PHP_LDFLAGS -Wl,-O1 -pie
PHP_SHA256 0d3270bbce4d9ec617befce52458b763fd461d475f1fe2ed878bb8573faed327
PHP_URL https://www.php.net/distributions/php-8.4.5.tar.xz
PHP_VERSION 8.4.5
PHPIZE_DEPS autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c

Why not use php:<version>-apache image with PHP and integrated web server (doc)?

I wanted to share the image description’s link, but I realized @bluepuma77 already shared it, so I will react to the reason of the exiting container

You used the default image variant which is just the cli. The cli can start a php shell. That has to be executed interactively or it will stop as bash or any shell would. If the process stops in the container, the container stops as the container is the isolation of the process. There is no container without a process in it.

You have to use either PHP FPM and configure HTTPD to acces the FPM endpoint or use apache variant of the php image that contains both. The apache variant would be the easiest and quickest, and you can learn about PHP FPM later which could be needed in production or when you don’t want to run HTTPD, only nginx.

The image description shows some examples, but it also links the full list of image variants

https://github.com/docker-library/docs/blob/master/php/README.md#supported-tags-and-respective-dockerfile-links

Thanks for the suggestion. Thru Portainer, that never seemed to be an option–though it makes perfect sense, and sounds easy & quick. I’m still untangling in my mind how the whole docker thing works–and where files are stored. I’m not married to doing things thru Portainer (I installed mariadb in a container from CLI, having found the CLI install syntax). I will try your suggestion.

I sensed from the log that it was CLI, but thought perhaps that I’d omitted a parameter in the install. I’ll read what you’ve offer.
Thanks both.

I looked at the doc you offered and saw " php:<version>-apache" in the middle of the doc, but with apologies for my lack of experience–I don’t know how to implement it without some sort of a step-by-step. If you have time, may I ask you to help me with that. Once I see your approach, I can learn and use your methodology. I’m not perfectly new to all this–I worked with a Unix-based database for a healthcare application decades ago (I can hear you laugh) & have built a dual boot linux laptop (again, years ago). Granted, small stuff. Thanks for any further guidance.

Seriously, it’s all in the doc. Just place your html and PHP files in the right directory:

$ docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:8.2-apache

With apologies, it is not clear to me what that does – but I will execute that line, where:
–name = the name I give it in the space “my-apache-php-app”
"$PWD is what? an existing password or one that I designate–and for what UID?

in the meantime, I tried to install php-apache and there’s config problems (image). Clearly ports are not mapped. And it is not clear to me that php-apache is just an adjunct, communicating w/ Apache, or if both are combined in one container. I’m simply not clear of the scope of this

I recommend to read a tutorial on how to get started with Docker. php:<version>-apache image is a full apache web-server with PHP enabled.

$             <- on command line
docker run    <- Docker run command
-d            <- daemonize (run in background)
-p 80:80      <- publish port 80
--name my-apache-php-app    <- container name
-v "$PWD":/var/www/html     <- bind mount host working directory to web server folder
php:8.2-apache    <- php image with web server

I am grateful for your patience–I realized when I wrote the previous post how intensely clueless i would appear. Your explanation of each switch (probably not the right parlance) is very helpful. Thank you.


Thanks again, for your time and knowledge.

I verified that the php-apache container is running, but php is not being parsed as it should, for some reason.
my test page:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
    <h3>PHP below</h3>
        <?php echo '<p>Hello World</p>';
phpinfo();
 ?>
    </body>
</html>

However the php does not seem to parse. Here’s the output:

I looked at the container to see that the environment variables include:
PHP_INI_DIR = /usr/local/etc/php
However when I went to cat that file it doesn’t exist.
I do not know how to begin troubleshooting.
Any thoughts welcome.

Your URL on top says “File”, so it seems you opened the file from folder, not from web-server container.

Thanks for your continued help. Your observation is correct - I thought since Apache is running and the container is bridged, it would resolve the file–so I continue to be unable to understand how docker containers worked. As I looked at another step-by-step for installing Apache in a container, toward the end of the instructions, a step says, “Open a brower and type in the IP address of your Docker container”, and what is supposed to be displayed is a rudimentary welcome web page. I type the address of my container in a browser and the response is “403 Forbidden. You don’t have permission to access this resource…”
Is there some place I need to add a UID/PSK?

It has nothing to do with Docker. If you opena file, you open a file, not connecting to a webserver that intrprets the file and returns a processed response. So that is one reason why PHP was not interpreted. You will also need to name it index.php instead of using the html extension. This is also webserver and PHP related. I assume you did not reconfigure the HTTP server to interpret html files as PHP files, so you still need to follow the rules and filenaming convention.

This can happen when you incorrectly place the file and the document root is empty or the file is not readable. Apache HTTPD by default searches for index.html or index.php if PHP is enabled properly (which is done in the image you are using). If I remember correctly, if the folder is empty and directory listing is not enabled (I don’t remember the default setting), you will get the error message you got, because you don’t have permission to list files in the folder and the server doesn’t find any default file to open. You can share your entire config so someone can recognize what you missed. By entire config I mean the commands you used to start the container, files you have, the folder structure, or in case you did anything on a GUI, what you did and what you clicked on.

By the way, I didn’t want to share it before, because my images are not regurarly updated, and not long ago some images in the tutorials were missing, but here is the temlate I usually share if someone needs to learn container basics (you will not go far without understanding it and it is really hard to explain everything in a chat to everyone). You will find “My tutorials” which contains copy-pastable guide for using HTTPD+PHP in a compose file. Note that command line is needed and it was mostly tested on Linux and MacOS.

Recommended links to learn the basics and concepts:

1 Like

will do.

That first page was literally copied / pasted from https://www.php.net/manual/en/tutorial.firstpage.php so, other than the other myriad of stuff I got wrong, i thought it would work.

In my defense, I wrote a php-based website many years ago which captures user input into a mysql database that still functions on GoDaddy’s platform, but it’s gone into disuse since my mother passed–and it has been a goodly long time.

For me the Docker world is foreign (I can hear you say ‘duh’ from here). and no matter how I research I find myself with an incomplete solution–php-apache, included. I (thought) I did exactly as instructed, but I’m not getting it. I’ll get there, hopefully.

Thanks for your patience and your help.

For whatever it’s worth: I followed the Introduction to Containers until p231. There was not enough info/background for me to be successful at the exercise–even when I cheated and looked at petazzo’s site, but I understood a bit more.
So, I went searching for more examples (replace the underscores w/a period):

medium_com/novai-php-laravel-101/running-php-using-docker-a-step-by-step-guide-ebfe044fcd43
explains it differently than
www.howtogeek_com/devops/how-to-use-docker-to-containerise-php-and-apache/
which is different again from
dockerwebdev_com/tutorials/docker-php-development/
which is not the same as
www.youtube_com/watch?v=ThpnqYpvnIM&t=9s

In each case, I’ll get so far, but either, subdirectory structure doesn’t match, or permissions don’t match and I have to use sudo to create directories which are assumed to have existed before the docker build. In that last example (YouTube) I was successful until the point where one goes to “localhost:80” which should have displayed a simple message, but I got “403 Forbidden”. In all of these cases, when the chain of instruction is broken because my screen does not match the intended outcome displayed in the instruction, there is simply no pathway to troubleshoot. Is there any place that explains “when you do this, you should find this here”, i.e., verification that that step was completed correctly.

thanks for any suggestions/direction. I am smart enough to “get there”.