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