docker:使用mariadb二进制包构建镜像

docker是容器技术,能方便的实现应用程序快速打包和部署,运行在Linux主机(也支持Windows),每个运行的容器相互隔离。

本例构建docker的mysql镜像,以方便的进行mysql功能测试等。
1、创建Dockerfile
   
  1. mkdir anthony
  2. touch Dockerfile
  3. vim Dockerfile 
文件内容:
   
  1. FROM centos:centos6
  2. MAINTAINER AnthonyYau <q_yxian@163.com>
  3. RUN yum -y install tar libaio vim
  4. ADD mariadb-10.0.12-linux-x86_64.tar.gz /usr/local
  5. RUN ln -s /usr/local/mariadb-10.0.12-linux-x86_64 /usr/local/mysql
  6. RUN useradd -M -s /sbin/nologin mysql
  7. RUN chown mysql:mysql -R /usr/local/mysql
  8. RUN cp /usr/local/mysql/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
  9. RUN cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
  10. RUN chown -R root /usr/local/mysql
  11. RUN chown -R mysql /usr/local/mysql/data
  12. RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  13. RUN chmod +x /etc/init.d/mysql
  14. RUN echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile.d/mysql.sh

FROM是必须的,指定镜像的在哪个基础上进行构建

2、构建镜像
   
  1. docker build --force-rm=True -t mariadb10.0.12 .
-t指定标签

输出信息:
   
  1. Uploading context 296.8 MB
  2. Uploading context 
  3. Step 0 : FROM centos:centos6
  4.  ---> b1bd49907d55
  5. Step 1 : MAINTAINER AnthonyYau <q_yxian@163.com>
  6.  ---> Running in 326680f12f79
  7.  ---> 4b5749e211d0
  8. Removing intermediate container 326680f12f79
  9. Step 2 : RUN yum -y install tar libaio
  10.  ---> Running in 63515833e6e0
  11. Loaded plugins: fastestmirror
  12. Setting up Install Process
  13. Resolving Dependencies
  14. --> Running transaction check
  15. ---> Package libaio.x86_64 0:0.3.107-10.el6 will be installed
  16. ---> Package tar.x86_64 2:1.23-11.el6 will be installed
  17. --> Finished Dependency Resolution
  18. Dependencies Resolved
  19. ================================================================================
  20.  Package         Arch            Version                    Repository     Size
  21. ================================================================================
  22. Installing:
  23.  libaio          x86_64          0.3.107-10.el6             base           21 k
  24.  tar             x86_64          2:1.23-11.el6              base          809 k
  25. Transaction Summary
  26. ================================================================================
  27. Install       2 Package(s)
  28. Total download size: 830 k
  29. Installed size: 2.5 M
  30. Downloading Packages:
  31. --------------------------------------------------------------------------------
  32. Total                                           172 kB/s | 830 kB     00:04     
  33. warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
  34. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
  35. Importing GPG key 0xC105B9DE:
  36.  Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
  37.  Package: centos-release-6-5.el6.centos.11.2.x86_64 (@Updates/$releasever)
  38.  From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
  39. Running rpm_check_debug
  40. Running Transaction Test
  41. Transaction Test Succeeded
  42. Running Transaction
  43. Warning: RPMDB altered outside of yum.
  44.   Installing : libaio-0.3.107-10.el6.x86_64                                 1/2 
  45.   Installing : 2:tar-1.23-11.el6.x86_64                                     2/2 
  46.   Verifying  : 2:tar-1.23-11.el6.x86_64                                     1/2 
  47.   Verifying  : libaio-0.3.107-10.el6.x86_64                                 2/2 
  48. Installed:
  49.   libaio.x86_64 0:0.3.107-10.el6            tar.x86_64 2:1.23-11.el6           
  50. Complete!
  51.  ---> aa5cd41ad1cf
  52. Removing intermediate container 63515833e6e0
  53. Step 3 : ADD mariadb-10.0.12-linux-x86_64.tar.gz /usr/local
  54.  ---> f716c7598e82
  55. Removing intermediate container 4df9bdc975f7
  56. Step 4 : RUN ln -s /usr/local/mariadb-10.0.12-linux-x86_64 /usr/local/mysql
  57.  ---> Running in e095f7b09b62
  58.  ---> 390c8679d083
  59. Removing intermediate container e095f7b09b62
  60. Step 5 : RUN useradd -M -s /sbin/nologin mysql
  61.  ---> Running in 49b479acf2ae
  62.  ---> b53b056c84eb
  63. Removing intermediate container 49b479acf2ae
  64. Step 6 : RUN chown mysql:mysql -R /usr/local/mysql
  65.  ---> Running in e6083ed5ae37
  66.  ---> cfd75d85ecab
  67. Removing intermediate container e6083ed5ae37
  68. Step 7 : RUN cp /usr/local/mysql/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
  69.  ---> Running in 7ccb60637d99
  70.  ---> a40435ceb990
  71. Removing intermediate container 7ccb60637d99
  72. Step 8 : RUN cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
  73.  ---> Running in a30888252206
  74. Installing MariaDB/MySQL system tables in './data' ...
  75. 2014-08-26 14:20:33 7f7e5fe51760 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
  76. 140826 14:20:33 [Note] InnoDB: Using mutexes to ref count buffer pool pages
  77. 140826 14:20:33 [Note] InnoDB: The InnoDB memory heap is disabled
  78. 140826 14:20:33 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  79. 140826 14:20:33 [Note] InnoDB: Compressed tables use zlib 1.2.3
  80. 140826 14:20:33 [Note] InnoDB: Using Linux native AIO
  81. 140826 14:20:33 [Note] InnoDB: Using CPU crc32 instructions
  82. 140826 14:20:33 [Note] InnoDB: Initializing buffer pool, size = 2.0G
  83. 140826 14:20:33 [Note] InnoDB: Completed initialization of buffer pool
  84. 140826 14:20:33 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
  85. 140826 14:20:33 [Note] InnoDB: Setting file ./ibdata1 size to 10 MB
  86. 140826 14:20:33 [Note] InnoDB: Database physically writes the file full: wait...
  87. 140826 14:20:33 [Note] InnoDB: Setting log file ./ib_logfile101 size to 256 MB
  88. InnoDB: Progress in MB: 100 200
  89. 140826 14:20:38 [Note] InnoDB: Setting log file ./ib_logfile1 size to 256 MB
  90. InnoDB: Progress in MB: 100 200
  91. 140826 14:20:42 [Note] InnoDB: Setting log file ./ib_logfile2 size to 256 MB
  92. InnoDB: Progress in MB: 100 200
  93. 140826 14:20:47 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
  94. 140826 14:20:47 [Warning] InnoDB: New log files created, LSN=45781
  95. 140826 14:20:47 [Note] InnoDB: Doublewrite buffer not found: creating new
  96. 140826 14:20:48 [Note] InnoDB: Doublewrite buffer created
  97. 140826 14:20:49 [Note] InnoDB: 128 rollback segment(s) are active.
  98. 140826 14:20:50 [Warning] InnoDB: Creating foreign key constraint system tables.
  99. 140826 14:20:50 [Note] InnoDB: Foreign key constraint system tables created
  100. 140826 14:20:50 [Note] InnoDB: Creating tablespace and datafile system tables.
  101. 140826 14:20:50 [Note] InnoDB: Tablespace and datafile system tables created.
  102. 140826 14:20:50 [Note] InnoDB: Waiting for purge to start
  103. 140826 14:20:50 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.17-65.0 started; log sequence number 0
  104. 140826 14:20:50 [Warning] Failed to load slave replication state from table mysql.gtid_slave_pos: 1146: Table 'mysql.gtid_slave_pos' doesn't exist
  105. 140826 14:21:07 [Note] InnoDB: FTS optimize thread exiting.
  106. 140826 14:21:07 [Note] InnoDB: Starting shutdown...
  107. 140826 14:21:10 [Note] InnoDB: Shutdown completed; log sequence number 1616704
  108. OK
  109. Filling help tables...
  110. 2014-08-26 14:21:10 7f65b9101760 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
  111. 140826 14:21:10 [Note] InnoDB: Using mutexes to ref count buffer pool pages
  112. 140826 14:21:10 [Note] InnoDB: The InnoDB memory heap is disabled
  113. 140826 14:21:10 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  114. 140826 14:21:10 [Note] InnoDB: Compressed tables use zlib 1.2.3
  115. 140826 14:21:10 [Note] InnoDB: Using Linux native AIO
  116. 140826 14:21:10 [Note] InnoDB: Using CPU crc32 instructions
  117. 140826 14:21:10 [Note] InnoDB: Initializing buffer pool, size = 2.0G
  118. 140826 14:21:10 [Note] InnoDB: Completed initialization of buffer pool
  119. 140826 14:21:10 [Note] InnoDB: Highest supported file format is Barracuda.
  120. 140826 14:21:10 [Note] InnoDB: 128 rollback segment(s) are active.
  121. 140826 14:21:10 [Note] InnoDB: Waiting for purge to start
  122. 140826 14:21:10 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.17-65.0 started; log sequence number 1616704
  123. 140826 14:21:11 [Note] InnoDB: FTS optimize thread exiting.
  124. 140826 14:21:11 [Note] InnoDB: Starting shutdown...
  125. 140826 14:21:13 [Note] InnoDB: Shutdown completed; log sequence number 1616714
  126. OK
  127. To start mysqld at boot time you have to copy
  128. support-files/mysql.server to the right place for your system
  129. PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
  130. To do so, start the server, then issue the following commands:
  131. './bin/mysqladmin' -u root password 'new-password'
  132. './bin/mysqladmin' -u root -h 24d6adf7503e password 'new-password'
  133. Alternatively you can run:
  134. './bin/mysql_secure_installation'
  135. which will also give you the option of removing the test
  136. databases and anonymous user created by default.  This is
  137. strongly recommended for production servers.
  138. See the MariaDB Knowledgebase at http://mariadb.com/kb or the
  139. MySQL manual for more instructions.
  140. You can start the MariaDB daemon with:
  141. cd '.' ; ./bin/mysqld_safe --datadir='./data'
  142. You can test the MariaDB daemon with mysql-test-run.pl
  143. cd './mysql-test' ; perl mysql-test-run.pl
  144. Please report any problems at http://mariadb.org/jira
  145. The latest information about MariaDB is available at http://mariadb.org/.
  146. You can find additional information about the MySQL part at:
  147. http://dev.mysql.com
  148. Support MariaDB development by buying support/new features from
  149. SkySQL Ab. You can contact us about this at sales@skysql.com.
  150. Alternatively consider joining our community based development effort:
  151. http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
  152.  ---> 783ee8a4ed80
  153. Removing intermediate container a30888252206
  154. Step 9 : RUN chown -R root /usr/local/mysql
  155.  ---> Running in 3e33b22b45b9
  156.  ---> e58e4f08e82a
  157. Removing intermediate container 3e33b22b45b9
  158. Step 10 : RUN chown -R mysql /usr/local/mysql/data
  159.  ---> Running in 7ff915c93d6f
  160.  ---> bef48e9f769e
  161. Removing intermediate container 7ff915c93d6f
  162. Step 11 : RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  163.  ---> Running in ea249def0eca
  164.  ---> ce96d94a5e58
  165. Removing intermediate container ea249def0eca
  166. Step 12 : RUN chmod +x /etc/init.d/mysql
  167.  ---> Running in 1f74fe9dc89c
  168.  ---> 7a89c7b8c155
  169. Removing intermediate container 1f74fe9dc89c
  170. Step 13 : RUN echo "PATH=$PATH:/usr/local/mysql/mysql/bin" >> /etc/profile.d/mysql.sh
  171.  ---> Running in a4a3c33c2c7d
  172.  ---> cb746a9ae90f
  173. Removing intermediate container a4a3c33c2c7d
  174. Successfully built cb746a9ae90f  #构建成功

3、查看所有可用镜像
   
  1. docker images
输出信息:
   
  1. REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
  2. mariadb10.0.12      latest              cb746a9ae90f        About a minute ago   2.182 GB
  3. centos              centos6             b1bd49907d55        3 weeks ago          212.5 MB
  4. centos              centos7             b157b77b1a65        3 weeks ago          243.7 MB
  5. centos              latest              b157b77b1a65        3 weeks ago          243.7 MB

4、运行容器,使用刚才构建的镜像
   
  1. docker run -t -i 4a2a4d05f753 bash

   
  1. bash-4.1# /etc/init.d/mysql start #启动mysql服务
  2. Starting MySQL... SUCCESS! 
  3. bash-4.1# netstat -ntlp  #查看进程监听
  4. Active Internet connections (only servers)
  5. Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
  6. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      -                                 
  7. bash-4.1# . /etc/profile
  8. bash-4.1# mysql  #验证mysql是否可访问
  9. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  10. Your MariaDB connection id is 4
  11. Server version: 10.0.12-MariaDB-log MariaDB Server
  12. Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. MariaDB [(none)]> 

5、查看所有容器,包括没有运行的,不加-a将只显示运行的容器
   
  1. docker ps -a
输出信息:
   
  1. CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                     PORTS               NAMES
  2. 4a2a4d05f753        mariadb10.0.12:latest   bash                7 minutes ago       Exited (0) 3 seconds ago                       agitated_pike8      

6、停止运行的容器
   
  1. docker stop 4a2a4d05f753

7、删除容器
   
  1. # docker rm 4a2a4d05f753
  2. 4a2a4d05f753

8、删除镜像,需要先删除运行在镜像上的容器
   
  1. # docker images
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
  3. mariadb10.0.12      latest              4a2a4d05f753        9 minutes ago       2.182 GB
  4. centos              centos6             b1bd49907d55        3 weeks ago         212.5 MB
  5. centos              centos7             b157b77b1a65        3 weeks ago         243.7 MB
  6. centos              latest              b157b77b1a65        3 weeks ago         243.7 MB
运行docker rmi删除镜像:
   
  1. # docker rmi  4a2a4d05f753
  2. Untagged: mariadb10.0.12:latest
  3. Deleted: cb746a9ae90f2976ba1a834313e586cfa9365d4a8cd918f8742772ea84d3d8d1
  4. Deleted: 7a89c7b8c155ba2b4beedacafecf72cfc1c838a0315d698acd3591cf19fcafd7
  5. Deleted: ce96d94a5e5873032a345c69eff521e5a10322bc3337c91df002ebce61cd63d9
  6. Deleted: bef48e9f769e4413267d9239a3b0543db06fee6a845c51f643e3b1aca9b1e058
  7. Deleted: e58e4f08e82a93e43a1f44c37431b983a4bb12ee3e678a6862db0946ed7e8102
  8. Deleted: 783ee8a4ed80ae59e9959cec2eadf968e685d5bb41986578b59973b0ea2514a4
  9. Deleted: a40435ceb9908a452c456914ac0f1e84a0fc64c6770ac89d39bdd4e2e9a0d51e
  10. Deleted: cfd75d85ecab2eea45da81100a5caa5b4f99b885191973d5e60316565dc6f654
  11. Deleted: b53b056c84ebd29aee83f031e186cd89cb84e55c0cddde2222290f6bee7c8516
  12. Deleted: 390c8679d083d65717dedcca344e1451153b75138c0475b5f75efc8867e4ef9e
  13. Deleted: f716c7598e826afa7f5462e2ca8e1049a02b936d1d167a02e362b14df3909c89
  14. Deleted: aa5cd41ad1cf3a3a9fdd1850e41cae570a6ddcf5a8c42e87bc84cd59188b4ec0
  15. Deleted: 4b5749e211d01d20cd15f8e2e45f511cd3624045c8e2d1f4313cbc66c4b5535c

   
  1. # docker images
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
  3. centos              centos6             b1bd49907d55        3 weeks ago         212.5 MB
  4. centos              centos7             b157b77b1a65        3 weeks ago         243.7 MB
  5. centos              latest              b157b77b1a65        3 weeks ago         243.7 MB

参考:





转载于:https://my.oschina.net/anthonyyau/blog/306854

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值