MySQL scripts in docker-entrypoint-initdb are not executed

You should clear data_volume before run the container and the sql files will be executed. This volume data_volume can be removed by using command: docker volume rm data_volume.

The root cause of your problem can be found in docker-entrypoint.sh. When you run a mysql container, it checks mysql directory /var/lib/mysql exist or not.
If the directory doesn’t exist (run it first time), it will run your SQL files.

    if [ ! -d "$DATADIR/mysql" ]; then
        //Some other logic here

        for f in /docker-entrypoint-initdb.d/*; do
            case "$f" in
                *.sh)     echo "$0: running $f"; . "$f" ;;
                *.sql)    echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
                *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
                *)        echo "$0: ignoring $f" ;;
            esac
            echo
        done 

You can get more details at Dockerfile source

Leave a Comment