I docker-entrypoint.sh-scriptet til det officielle docker-billede af postgres står skrevet:
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
Så hver .sql-fil, du vil køre i dit docker-billede, kan bare placeres i den mappe. Så min dockerfil ser ud som
FROM postgres:9.3
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker
ADD CreateDB.sql /docker-entrypoint-initdb.d/
Og indholdet af min CreateDB.sql:
CREATE TABLE web_origins (
client_id character varying(36) NOT NULL,
value character varying(255)
);
Så jeg starter bare min container med:
docker run -d my-postgres
For at kontrollere:
docker exec -it 6250aee43d12 bash
[email protected]:/# psql -h localhost -p 5432 -U docker -d docker
psql (9.3.13)
Type "help" for help.
docker=# \c
You are now connected to database "docker" as user "docker".
docker=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+--------
public | web_origins | table | docker
(1 row)
Du kan finde detaljerne for mysql her i denne blog .