Reputation: 883
I have created a Dockerfile to have a development environment on my computer with php7, mysql and some other library.
The apache service starts automatically when a run the container but I can't get the same results with mysql. The last sentence of my Dockerfile looks like this CMD ["bash", "run.sh"]
and then in run.sh I have this:
#!/bin/bash
set -e
PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php/7.0/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php/7.0/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php/7.0/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php/7.0/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php/7.0/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php/7.0/cli/php.ini
source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND
chmod 0755 start.sh
sleep 1
start.sh
/bin/bash
That calls a file named start.sh that looks like this:
#!/usr/bin/env bash
# Mysql
sed -i -e 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
service mysql start
It is divided in two files because I tested everything but I can't get it working. If I do a docker exec inside the container and I start mysql everything works fine.
Any idea of what I am doing wrong?
Upvotes: 0
Views: 1125
Reputation: 37964
You are running exec /usr/sbin/apache2 -DFOREGROUND
relatively early in the run.sh
script. The thing about exec
is that it does not start a sub-process of the shell, but it replaces the shell with a new process.
From the man page:
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command.
This means that your shell script will not continue running after invoking exec
.
I could now suggest a number of ways to hack around this with Bash, but all of these will be (in my opinion) messy and difficult to maintain. If you want to manage multiple processes within one container (like Apache and MySQL), I strongly suggest having a look at process managers like supervisord. There's an excellent article in the official documentation that even uses a similar use case to yours as an example.
Upvotes: 1
Reputation: 9111
start.sh is given with no absolute or relative path, like if it was in your PATH, is it ?
You could add some "echo 'I am actually called' > log.txt" at the beginning of start.sh to check if it's even called.
Upvotes: 0