if you're using official mysql docker image then
https://hub.docker.com/_/mysql
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions **.sh, .sql and .sql.gz** that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. You can easily populate your mysql services by mounting a SQL
dump into that directory and provide custom images with contributed
data. SQL files will be imported by default to the database specified
by the MYSQL_DATABASE variable.
Here is example, you can even run custom shell scripts, with shared volume your php container can easily access file generated.
version : '3'
services:
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: mysecretpassword
ports:
- "3306:3306"
volumes:
- "./scripts/add-my-schema.sql:/docker-entrypoint-initdb.d/1.sql"
- "./scripts/add-data-to-it.sql:/docker-entrypoint-initdb.d/2.sql"
- "./scripts/generate-some-output.sh:/docker-entrypoint-initdb.d/process.sh"
Here is reference to https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh#L56
It contains
# usage: docker_process_init_files [file [file [...]]]
# ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions
docker_process_init_files() {
# mysql here for backwards compatibility "${mysql[@]}"
mysql=( docker_process_sql )
echo
local f
for f; do
case "$f" in
*.sh)
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
mysql_note "$0: running $f"
"$f"
else
mysql_note "$0: sourcing $f"
. "$f"
fi
;;
*.sql) mysql_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
*.sql.gz) mysql_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
*.sql.xz) mysql_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
*) mysql_warn "$0: ignoring $f" ;;
esac
echo
done
}