Initializing DB\'s Running in Containers PDF

Title Initializing DB\'s Running in Containers
Author Arohi Gupta
Course Cloud Technologies
Institution University of Colorado Boulder
Pages 2
File Size 67.9 KB
File Type PDF
Total Downloads 39
Total Views 125

Summary

This section includes the php script required to initialize the db container. ...


Description

One thing we’ve found in this course is that initializing a DB with automation usually involves some tricks. With Docker things are no different. I’ve seen a number of approaches so far on how to accomplish this: ● Executing create calls in the PHP script ● Using scripts passed in to the Dockerfile as part of docker build ● Entrypoint scripts ran as part of startup of the Container It turns out there’s a better way to do this. You don’t have to modify the base mysql or mariadb image at all. There are a number of things you can pass in via startup flags from both Docker and docker-compose files. Even better though, databases like mysql, mariadb and postgres and even mongodb have a special mechanism for passing in initialization info: When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .  sh, .  sql and .  sql.gz that are found in /  docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your m  ariadb services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the M  YSQL_DATABASE variable. So, in your directory with your compose file you can create a sub-directory: docker-entrypoint-initdb.d

Then add your initialization scripts (ie an sql script) to that directory. Then bind-mount that directory in to the DB container at /docker-entrypoint-initdb.d `docker run -v docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d mariadb`

The base db images from Dockerhub are built such that they will check this directory for any files and it will automatically run any files in this directory on startup. You can create an sql file to not only create the database and it’s settings, but also create tables, add entries etc. This means you don’t have to create a custom image, and you don’t have to do any extra work to populate the data. Example sql file: CREATE DATABASE mydb; GRANT ALL PRIVILEGES ON mydb.* TO 'user'@'localhost' \ IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON mydb.* TO 'user'@'%' \ IDENTIFIED BY 'password';

NOTE: mongodb is slightly different, you won’t use an sql script like the one above but you’ll do mongo specific scripts in this same directory. Also, remember mongo doesn’t require you to create the db ahead of time....


Similar Free PDFs