Ve skriptu docker-entrypoint.sh oficiálního obrázku dockeru z postgres je napsáno:
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
Takže každý soubor .sql, který chcete spustit uvnitř obrazu dockeru, lze umístit do této složky. Můj dockerfile tedy vypadá takto
FROM postgres:9.3
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker
ADD CreateDB.sql /docker-entrypoint-initdb.d/
A obsah mého CreateDB.sql:
CREATE TABLE web_origins (
client_id character varying(36) NOT NULL,
value character varying(255)
);
Takže začnu svůj kontejner s:
docker run -d my-postgres
Pro kontrolu:
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)
Podrobnosti o mysql najdete zde v tomto blogu .