Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. FROM mysql
  2. MAINTAINER (me) <email>
  3.  
  4. # Copy the database schema to the /data directory
  5. COPY files/epcis_schema.sql /data/epcis_schema.sql
  6.  
  7. # Change the working directory
  8. WORKDIR data
  9.  
  10. CMD mysql -u $MYSQL_USER -p $MYSQL_PASSWORD $MYSQL_DATABASE < epcis_schema.sql
  11.  
  12. docker run --name ${CONTAINER_NAME} -e MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} -e MYSQL_USER=${DB_USER} -e MYSQL_PASSWORD=${DB_USER_PASSWORD} -e MYSQL_DATABASE=${DB_NAME} -d mvpgomes/epcisdb
  13.  
  14. docker build .
  15.  
  16. create database test;
  17. use test;
  18.  
  19. CREATE TABLE testtab
  20. (
  21. id INTEGER AUTO_INCREMENT,
  22. name TEXT,
  23. PRIMARY KEY (id)
  24. ) COMMENT='this is my test table';
  25.  
  26. #!/bin/bash
  27.  
  28. # Initialize MySQL database.
  29. # ADD this file into the container via Dockerfile.
  30. # Assuming you specify a VOLUME ["/var/lib/mysql"] or `-v /var/lib/mysql` on the `docker run` command…
  31. # Once built, do e.g. `docker run your_image /path/to/docker-mysql-initialize.sh`
  32. # Again, make sure MySQL is persisting data outside the container for this to have any effect.
  33.  
  34. set -e
  35. set -x
  36.  
  37. mysql_install_db
  38.  
  39. # Start the MySQL daemon in the background.
  40. /usr/sbin/mysqld &
  41. mysql_pid=$!
  42.  
  43. until mysqladmin ping >/dev/null 2>&1; do
  44. echo -n "."; sleep 0.2
  45. done
  46.  
  47. # Permit root login without password from outside container.
  48. mysql -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION"
  49.  
  50. # create the default database from the ADDed file.
  51. mysql < /tmp/epcis_schema.sql
  52.  
  53. # Tell the MySQL daemon to shutdown.
  54. mysqladmin shutdown
  55.  
  56. # Wait for the MySQL daemon to exit.
  57. wait $mysql_pid
  58.  
  59. # create a tar file with the database as it currently exists
  60. tar czvf default_mysql.tar.gz /var/lib/mysql
  61.  
  62. # the tarfile contains the initialized state of the database.
  63. # when the container is started, if the database is empty (/var/lib/mysql)
  64. # then it is unpacked from default_mysql.tar.gz from
  65. # the ENTRYPOINT /tmp/run_db script
  66.  
  67. # start db
  68.  
  69. set -e
  70. set -x
  71.  
  72. # first, if the /var/lib/mysql directory is empty, unpack it from our predefined db
  73. [ "$(ls -A /var/lib/mysql)" ] && echo "Running with existing database in /var/lib/mysql" || ( echo 'Populate initial db'; tar xpzvf default_mysql.tar.gz )
  74.  
  75. /usr/sbin/mysqld
  76.  
  77. FROM mysql
  78. MAINTAINER (me) <email>
  79.  
  80. # Copy the database schema to the /data directory
  81. ADD files/run_db files/init_db files/epcis_schema.sql /tmp/
  82.  
  83. # init_db will create the default
  84. # database from epcis_schema.sql, then
  85. # stop mysqld, and finally copy the /var/lib/mysql directory
  86. # to default_mysql_db.tar.gz
  87. RUN /tmp/init_db
  88.  
  89. # run_db starts mysqld, but first it checks
  90. # to see if the /var/lib/mysql directory is empty, if
  91. # it is it is seeded with default_mysql_db.tar.gz before
  92. # the mysql is fired up
  93.  
  94. ENTRYPOINT "/tmp/run_db"
  95.  
  96. docker build --no-cache .
  97.  
  98. Sending build context to Docker daemon 7.168 kB
  99. Sending build context to Docker daemon
  100. Step 0 : FROM mysql
  101. ---> 461d07d927e6
  102. Step 1 : MAINTAINER (me) <email>
  103. ---> Running in 963e8de55299
  104. ---> 2fd67c825c34
  105. Removing intermediate container 963e8de55299
  106. Step 2 : ADD files/run_db files/init_db files/epcis_schema.sql /tmp/
  107. ---> 81871189374b
  108. Removing intermediate container 3221afd8695a
  109. Step 3 : RUN /tmp/init_db
  110. ---> Running in 8dbdf74b2a79
  111. + mysql_install_db
  112. 2015-03-19 16:40:39 12 [Note] InnoDB: Using atomics to ref count buffer pool pages
  113. ...
  114. /var/lib/mysql/ib_logfile0
  115. ---> 885ec2f1a7d5
  116. Removing intermediate container 8dbdf74b2a79
  117. Step 4 : ENTRYPOINT "/tmp/run_db"
  118. ---> Running in 717ed52ba665
  119. ---> 7f6d5215fe8d
  120. Removing intermediate container 717ed52ba665
  121. Successfully built 7f6d5215fe8d
  122.  
  123. docker run -d 7f6d5215fe8d
  124.  
  125. 4b377ac7397ff5880bc9218abe6d7eadd49505d50efb5063d6fab796ee157bd3
  126.  
  127. docker stop 4b377
  128. docker start 4b377
  129.  
  130. docker logs 4b377
  131.  
  132. Populate initial db
  133. var/lib/mysql/
  134. ...
  135.  
  136. Running with existing database in /var/lib/mysql
  137.  
  138. gregs-air:~ gfausak$ ls -Rl mdir
  139. total 8
  140. -rw-r--r-- 1 gfausak wheel 534 Mar 19 11:13 Dockerfile
  141. drwxr-xr-x 5 gfausak staff 170 Mar 19 11:24 files
  142.  
  143. mdir/files:
  144. total 24
  145. -rw-r--r-- 1 gfausak staff 126 Mar 19 11:14 epcis_schema.sql
  146. -rwxr-xr-x 1 gfausak staff 1226 Mar 19 11:16 init_db
  147. -rwxr-xr-x 1 gfausak staff 284 Mar 19 11:23 run_db
  148.  
  149. mysqldump -h <your_mysql_host> -u <user_name> -p --no-data <schema_name> > schema.sql
  150.  
  151. FROM mysql:5.7.15
  152.  
  153. MAINTAINER me
  154.  
  155. ENV MYSQL_DATABASE=<schema_name>
  156. MYSQL_ROOT_PASSWORD=<password>
  157.  
  158. ADD schema.sql /docker-entrypoint-initdb.d
  159.  
  160. EXPOSE 3306
  161.  
  162. docker-compose build
  163. docker-compose up
  164.  
  165. docker pull tutum/mysql:5.5
  166.  
  167. docker run -d -p 3306:3306 -v /tmp:/tmp -e STARTUP_SQL="/tmp/to_be_imported.mysql" tutum/mysql:5.5
  168.  
  169. docker logs #<CONTAINER_ID>
  170.  
  171. database:
  172. image: mariadb
  173. ports:
  174. - 3306:3306
  175. expose:
  176. - 3306
  177. volumes:
  178. - ./docker/mariadb/data:/var/lib/mysql:rw
  179. - ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
  180. environment:
  181. MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  182.  
  183. version: "3"
  184. services:
  185. db:
  186. container_name: db
  187. image: mysql
  188. ports:
  189. - "3306:3306"
  190. environment:
  191. - MYSQL_ROOT_PASSWORD=mysql
  192. - MYSQL_DATABASE=db
  193.  
  194. volumes:
  195. - /home/user/db/mysql/data:/var/lib/mysql
  196. - /home/user/db/mysql/init:/docker-entrypoint-initdb.d/:ro
  197.  
  198. CREATE DATABASE mydb;
  199. GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%' IDENTIFIED BY 'mysql';
  200. GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost' IDENTIFIED BY 'mysql';
  201. USE mydb
  202. CREATE TABLE CONTACTS (
  203. [ ... ]
  204. );
  205. INSERT INTO CONTACTS VALUES ...
  206. [ ... ]
  207.  
  208. mysql:
  209. from: mysql:5.7
  210. volumes:
  211. - ./database:/tmp/database
  212. command: mysqld --init-file="/tmp/database/install_db.sql"
  213.  
  214. version: "3"
  215.  
  216. services:
  217.  
  218. database:
  219. image: mysql:5.7
  220. container_name: mysql
  221. ports:
  222. - "3306:3306"
  223. volumes:
  224. - db-data:/docker-entrypoint-initdb.d
  225. environment:
  226. - MYSQL_DATABASE=sample
  227. - MYSQL_ROOT_PASSWORD=root
  228.  
  229. volumes:
  230. db-data:
  231. external: true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement