Reputation: 838
There are many questions on this site abut it but none of them give me a solution.
My dockerfile:
from php:7.2-apache
copy php.ini "$PHP_INI_DIR/php.ini"
copy symfony/ /var/www/html/
run chmod -R o+w /var/www/html/var/cache /var/www/html/var/logs /var/www/html/var/sessions
run docker-php-ext-install pdo_mysql opcache
copy exec.php /var/www/html/
my docker-compose.yml file:
version: '3'
services:
web:
build: .
ports:
- "8082:80"
depends_on:
- mysql
command: ["php", "/var/www/html/exec.php"]
#command: ["echo", "hello"]
mysql:
image: "mysql:5"
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
The problem comes from the command
key. When I comment the commands the php-apache
/web
runs. from docker ps
I can see the container. But when I add a command
php-apache
/web
container died on php-apache_web_1 exited with code 0
and it is not shown from docker ps
while the mysql
container exists and I can interact with it.
From that answer someone says the container exit because the command is completed his task and exited. But I don't want my server to exit or die/kill. How to prevent the command from returning an exit status without running the command forever?
If you're curious about why I am running a command, my command do almost exactly how explained there.
Edited: What I want is a solution on how to keep my php apache server running and create a database in the mysql server once the php apache image has built.
I edited my codes according to a suggested answer but exec "$@"
still exited with code 0:
The dockerfile:
from php:7.2-apache
copy php.ini "$PHP_INI_DIR/php.ini"
copy symfony/ /var/www/html/
run chmod -R o+w /var/www/html/var/cache /var/www/html/var/logs /var/www/html/var/sessions
run docker-php-ext-install pdo_mysql opcache
copy exec.php /var/www/html/
#using an sh script
copy run.sh /var/www/html/
run chmod u+x,g+x /var/www/html/run.sh
The docker-compose.yml file:
version: '3'
services:
web:
build: .
ports:
- "8082:80"
depends_on:
- mysql
command: ["/var/www/html/run.sh"]
mysql:
image: "mysql:5"
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
The run.sh script:
#!/bin/sh
set -e
# Execute your custom scripts
php -f /var/www/html/exec.php
#End with running the original command
exec "$@"
Then to run the script I execute in the terminal:
docker-compose down
docker-compose build
docker-compose up
Upvotes: 2
Views: 5416
Reputation: 838
Thanks to pat answer I was able to make apache run.
The line exec "$@"
did not work for me. "$@"
was a blank string. But thanks to the link he provided, I found the script needed to run the apache service which is /usr/sbin/apache2ctl -D FOREGROUND
. So instead of exec "$@"
I wrote exec /usr/sbin/apache2ctl -D FOREGROUND
at the end of the script.
The script is now:
#run.sh
#!/bin/sh
set -e
# Execute your custom scripts
php -f /var/www/html/exec.php
#End with running the original command
exec /usr/sbin/apache2ctl -D FOREGROUND
After taking a look at the image dockerfile or running docker inspect php:7.2-apache
I found ENTRYPOINT ["docker-php-entrypoint"]
and CMD ["apache2-foreground"]
.
So edit my script to exec docker-php-entrypoint apache2-foreground
to make sure the image script executes properly.
Upvotes: 2
Reputation: 1943
In order to run commands before the container starts executing its command you can use a custom entrypoint.
#!/bin/sh
set -e
# Execute your custom scripts
php -f /var/www/html/exec.php
#End with running the original command
exec "$@"
For more information check out the official documentation for entrypoint
Upvotes: 2
Reputation: 2295
You need to run a server and listen to a certain port in order keep the app running (listening to incoming requests). That's the idea behind keeping the server alive, because it receives requests. If your php file has a small task and it's done, It has no need to stay up.
So what you need is to have a web server and since you are using pure php, you may find this article useful about building web servers.
There is a quick hack for what you want to do which is to run php in interactive mode using -a
flag. Try to include the following in your docker-compose file:
command: ["php", "-a", "/var/www/html/exec.php"]
Upvotes: 0