├── setup.sql ├── Dockerfile └── README.md /setup.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE myexample; 2 | 3 | USE myexample; 4 | 5 | CREATE TABLE mytable (myfield VARCHAR(20)); 6 | 7 | INSERT INTO mytable VALUES ('Hello'), ('Dolly'); 8 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mariadb:latest as builder 2 | 3 | # That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init 4 | RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"] 5 | 6 | # needed for intialization 7 | ENV MYSQL_ROOT_PASSWORD=root 8 | 9 | COPY setup.sql /docker-entrypoint-initdb.d/ 10 | 11 | # Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume. 12 | # https://docs.docker.com/engine/reference/builder/#volume : 13 | # Changing the volume from within the Dockerfile: If any build steps change the data within the volume after 14 | # it has been declared, those changes will be discarded. 15 | RUN ["/usr/local/bin/docker-entrypoint.sh", "mariadbd", "--datadir", "/initialized-db", "--aria-log-dir-path", "/initialized-db"] 16 | 17 | FROM mariadb:latest 18 | 19 | COPY --from=builder /initialized-db /var/lib/mysql 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A prepopulated mysql container ? 2 | ================================ 3 | 4 | Yeah why not, i like my test faster :D 5 | 6 | But how ? 7 | --------- 8 | 9 | The mysql/mariadb container image contain an init script that will execute everything in `/docker-entrypoint-initdb.d/` 10 | 11 | see `Initializing a fresh instance` @ https://hub.docker.com/_/mariadb/ 12 | 13 | So let's run this initialization in a multi-stage build and copy the initialized DB in the new image :D 14 | 15 | (see comments in dockerfile for details of the hack) 16 | 17 | Try it! 18 | ====== 19 | 20 | Clone this, then... 21 | 22 | ``` 23 | }> docker build --tag my-prepopulated-image . 24 | ... 25 | 26 | }> docker run -d --rm --name my-container my-prepopulated-image 27 | ... 28 | 29 | }> docker logs my-container 30 | 31 | (there was is initialization here, therefore we win) 32 | 33 | 2018-06-08 21:15:55 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ... 34 | 2018-06-08 21:15:55 0 [Note] InnoDB: Using Linux native AIO 35 | 2018-06-08 21:15:55 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 36 | 2018-06-08 21:15:55 0 [Note] InnoDB: Uses event mutexes 37 | 2018-06-08 21:15:55 0 [Note] InnoDB: Compressed tables use zlib 1.2.8 38 | 2018-06-08 21:15:55 0 [Note] InnoDB: Number of pools: 1 39 | 2018-06-08 21:15:55 0 [Note] InnoDB: Using SSE2 crc32 instructions 40 | 2018-06-08 21:15:55 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M 41 | 2018-06-08 21:15:55 0 [Note] InnoDB: Completed initialization of buffer pool 42 | 2018-06-08 21:15:55 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 43 | 2018-06-08 21:15:56 0 [Note] InnoDB: 128 out of 128 rollback segments are active. 44 | 2018-06-08 21:15:56 0 [Note] InnoDB: Creating shared tablespace for temporary tables 45 | 2018-06-08 21:15:56 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 46 | 2018-06-08 21:15:56 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 47 | 2018-06-08 21:15:56 0 [Note] InnoDB: 10.3.7 started; log sequence number 1639605; transaction id 39 48 | 2018-06-08 21:15:56 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 49 | 2018-06-08 21:15:56 0 [Note] Plugin 'FEEDBACK' is disabled. 50 | 2018-06-08 21:15:56 0 [Note] Server socket created on IP: '::'. 51 | 2018-06-08 21:15:56 0 [Warning] 'proxies_priv' entry '@% root@9977cc179be0' ignored in --skip-name-resolve mode. 52 | 2018-06-08 21:15:56 0 [Note] InnoDB: Buffer pool(s) load completed at 180608 21:15:56 53 | 2018-06-08 21:15:56 0 [Note] Reading of all Master_info entries succeded 54 | 2018-06-08 21:15:56 0 [Note] Added new Master_info '' to hash table 55 | 2018-06-08 21:15:56 0 [Note] mysqld: ready for connections. 56 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution 57 | 58 | }> docker run -it --rm --link my-container mariadb:latest mariadb -hmy-container -uroot -proot myexample -e "select * from mytable;" 59 | +---------+ 60 | | myfield | 61 | +---------+ 62 | | Hello | 63 | | Dolly | 64 | +---------+ 65 | ``` 66 | --------------------------------------------------------------------------------