Adding a Database for Compose
Learn how to add a database to your Compose based environment.
If you’re not using Compose, follow the databases for Dockerfiles guide.
Start by creating a Dockerfile to build your service and adding it to your repository:
# Change version number to desired (i.e. 5.5, 5.6, 5.7) FROM runnable/mysql:5.6 # Set desired environment variables ENV MYSQL_USER mysql ENV MYSQL_PASSWORD mysql ENV MYSQL_DATABASE app # to set a root password, uncomment the next line # ENV MYSQL_ROOT_PASSWORD test # Uncomment the following ADD line to enable seeding the PostgreSQL DB # Make sure to check in a mysql dump file (i.e. mysqldump [options] > seed.sql) # ADD seed.sql /seed.sql # Run the initialization script (leave this alone) RUN gosu mysql /init.sh
# Change desired version below (Available: 9.3, 9.4, 9.5) FROM runnable/postgres:9.4 ENV POSTGRES_USER postgres ENV POSTGRES_DB postgres # Uncomment the following ADD line to enable seeding the PostgreSQL DB # Make sure to check in a pg_dump file (i.e. pg_dump -Fc db_name -f seed.dump) # ADD seed.dump /seed.dump # Run the initialization script (leave this alone) RUN gosu postgres /init.sh \ # Uncomment the following line for a custom pg_restore command. Edit as needed # "pg_restore --no-acl --no-owner -c -v -d $POSTGRES_DB /seed.dump"
Then add your database service to your Compose file:
Once you push your changes to your repository, you should see a new database service launch on the Containers page.
Note: If you’re using Docker Hub’s MySQL image, your output may differ due to their use of
volumes, which are unsupported features.
Seeding Your Database
Creating a Seed File
You can clone an existing database to create a seed file. Here’s how to create one:
$ mysqldump --all-databases -u mysql -p > seed.sql
$ pg_dump -U postgres -Fc postgres -f seed.dump
Note: The default password is
You are now the proud owner of a
Using the Seed File
Your database’s Dockerfile will need access to your seed file. Check the file into your repository and add this line to the Dockerfile before the
ADD [src] /seed.sql
ADD [src] /seed.dump
[src] with the path to your dump file, relative to your Dockerfile:
Note: Do not modify the destination
/seed.dump; it’s required to for the initialization script.
Your seeded database will now be created for all new builds.