Docker Community Forums

Share and learn in the Docker community.

Best practice tips php-apache and postgres/postgis environment

Hi,
I’m trying to set up a docker environment, with a php-apache container along with a postgres container. Actually I succeeded and it kind of works. Thing is I just got the server from a previous coworker, and I’ll eventually update it but for now I just want to make it work as it is.
So basically, my php container creates the database relations with symfony command doctrine:schema:create and my postgres container populates the data with a .sql script. I thought I could put these commands as entrypoint to each container, but the .sql script can only be executed once the symfony command has run and finished.
So for now I just run the .sql script by hand though docker exec -it <container id> once everything is set up, and it works, but can obviously not stay that way.

I am wondering what would be the best approach for this. Should I just put a script in my postgres container that will wait for the relations to exist in the db before running the .sql script? Should I add postgres to my php container in order to run all commands from there? Neither looks like a nice thing to do, and I’m sure this configuration is not something unusual.

I’m relativerly new to Docker and I am not sure I get the whole picture yet, so maybe I’m just talking nonsense and there’s no real issue to my situation. But if I could get a little pointer to which direction to go to it would be great.

Hi !
Thanks a bunch, I used the file_get_contents command in order to retrive SQL commands from my scripts and it works like a charm :ok_hand:
It looks something like that:

<?php
$dbh = pg_connect("host=host dbname=name port=5432 user=user password=password");
if (!$dbh) 
    { 
        die("Error in connection: " . pg_last_error()); 
    }

$sql = "SELECT * FROM Countries";
$check_db_populated = pg_query($dbh, $sql);
$row = pg_fetch_array($check_db_populated);

if(!$row[0])
{
    echo "need to populate";
    $result = pg_query($dbh, "BEGIN; COMMIT;\n" . file_get_contents('myscript.sql'));
    pg_free_result($result);
    echo "init db done";
}
else
{
    echo "dont need to populate";
}

pg_close($dbh); 
?>