docker是容器技术,能方便的实现应用程序快速打包和部署,运行在Linux主机(也支持Windows),每个运行的容器相互隔离。
本例构建docker的mysql镜像,以方便的进行mysql功能测试等。
1、创建Dockerfile
mkdir anthony
touch Dockerfile
vim Dockerfile
文件内容:
FROM centos:centos6
MAINTAINER AnthonyYau <q_yxian@163.com>
RUN yum -y install tar libaio vim
ADD mariadb-10.0.12-linux-x86_64.tar.gz /usr/local
RUN ln -s /usr/local/mariadb-10.0.12-linux-x86_64 /usr/local/mysql
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql -R /usr/local/mysql
RUN cp /usr/local/mysql/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
RUN cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
RUN chown -R root /usr/local/mysql
RUN chown -R mysql /usr/local/mysql/data
RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
RUN chmod +x /etc/init.d/mysql
RUN echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile.d/mysql.sh
FROM是必须的,指定镜像的在哪个基础上进行构建
2、构建镜像
docker build --force-rm=True -t mariadb10.0.12 .
-t指定标签
输出信息:
Uploading context 296.8 MB
Uploading context
Step 0 : FROM centos:centos6
---> b1bd49907d55
Step 1 : MAINTAINER AnthonyYau <q_yxian@163.com>
---> Running in 326680f12f79
---> 4b5749e211d0
Removing intermediate container 326680f12f79
Step 2 : RUN yum -y install tar libaio
---> Running in 63515833e6e0
Loaded plugins: fastestmirror
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.107-10.el6 will be installed
---> Package tar.x86_64 2:1.23-11.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
libaio x86_64 0.3.107-10.el6 base 21 k
tar x86_64 2:1.23-11.el6 base 809 k
Transaction Summary
================================================================================
Install 2 Package(s)
Total download size: 830 k
Installed size: 2.5 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 172 kB/s | 830 kB 00:04
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-5.el6.centos.11.2.x86_64 (@Updates/$releasever)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : libaio-0.3.107-10.el6.x86_64 1/2
Installing : 2:tar-1.23-11.el6.x86_64 2/2
Verifying : 2:tar-1.23-11.el6.x86_64 1/2
Verifying : libaio-0.3.107-10.el6.x86_64 2/2
Installed:
libaio.x86_64 0:0.3.107-10.el6 tar.x86_64 2:1.23-11.el6
Complete!
---> aa5cd41ad1cf
Removing intermediate container 63515833e6e0
Step 3 : ADD mariadb-10.0.12-linux-x86_64.tar.gz /usr/local
---> f716c7598e82
Removing intermediate container 4df9bdc975f7
Step 4 : RUN ln -s /usr/local/mariadb-10.0.12-linux-x86_64 /usr/local/mysql
---> Running in e095f7b09b62
---> 390c8679d083
Removing intermediate container e095f7b09b62
Step 5 : RUN useradd -M -s /sbin/nologin mysql
---> Running in 49b479acf2ae
---> b53b056c84eb
Removing intermediate container 49b479acf2ae
Step 6 : RUN chown mysql:mysql -R /usr/local/mysql
---> Running in e6083ed5ae37
---> cfd75d85ecab
Removing intermediate container e6083ed5ae37
Step 7 : RUN cp /usr/local/mysql/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
---> Running in 7ccb60637d99
---> a40435ceb990
Removing intermediate container 7ccb60637d99
Step 8 : RUN cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
---> Running in a30888252206
Installing MariaDB/MySQL system tables in './data' ...
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.
140826 14:20:33 [Note] InnoDB: Using mutexes to ref count buffer pool pages
140826 14:20:33 [Note] InnoDB: The InnoDB memory heap is disabled
140826 14:20:33 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
140826 14:20:33 [Note] InnoDB: Compressed tables use zlib 1.2.3
140826 14:20:33 [Note] InnoDB: Using Linux native AIO
140826 14:20:33 [Note] InnoDB: Using CPU crc32 instructions
140826 14:20:33 [Note] InnoDB: Initializing buffer pool, size = 2.0G
140826 14:20:33 [Note] InnoDB: Completed initialization of buffer pool
140826 14:20:33 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
140826 14:20:33 [Note] InnoDB: Setting file ./ibdata1 size to 10 MB
140826 14:20:33 [Note] InnoDB: Database physically writes the file full: wait...
140826 14:20:33 [Note] InnoDB: Setting log file ./ib_logfile101 size to 256 MB
InnoDB: Progress in MB: 100 200
140826 14:20:38 [Note] InnoDB: Setting log file ./ib_logfile1 size to 256 MB
InnoDB: Progress in MB: 100 200
140826 14:20:42 [Note] InnoDB: Setting log file ./ib_logfile2 size to 256 MB
InnoDB: Progress in MB: 100 200
140826 14:20:47 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
140826 14:20:47 [Warning] InnoDB: New log files created, LSN=45781
140826 14:20:47 [Note] InnoDB: Doublewrite buffer not found: creating new
140826 14:20:48 [Note] InnoDB: Doublewrite buffer created
140826 14:20:49 [Note] InnoDB: 128 rollback segment(s) are active.
140826 14:20:50 [Warning] InnoDB: Creating foreign key constraint system tables.
140826 14:20:50 [Note] InnoDB: Foreign key constraint system tables created
140826 14:20:50 [Note] InnoDB: Creating tablespace and datafile system tables.
140826 14:20:50 [Note] InnoDB: Tablespace and datafile system tables created.
140826 14:20:50 [Note] InnoDB: Waiting for purge to start
140826 14:20:50 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.17-65.0 started; log sequence number 0
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
140826 14:21:07 [Note] InnoDB: FTS optimize thread exiting.
140826 14:21:07 [Note] InnoDB: Starting shutdown...
140826 14:21:10 [Note] InnoDB: Shutdown completed; log sequence number 1616704
OK
Filling help tables...
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.
140826 14:21:10 [Note] InnoDB: Using mutexes to ref count buffer pool pages
140826 14:21:10 [Note] InnoDB: The InnoDB memory heap is disabled
140826 14:21:10 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
140826 14:21:10 [Note] InnoDB: Compressed tables use zlib 1.2.3
140826 14:21:10 [Note] InnoDB: Using Linux native AIO
140826 14:21:10 [Note] InnoDB: Using CPU crc32 instructions
140826 14:21:10 [Note] InnoDB: Initializing buffer pool, size = 2.0G
140826 14:21:10 [Note] InnoDB: Completed initialization of buffer pool
140826 14:21:10 [Note] InnoDB: Highest supported file format is Barracuda.
140826 14:21:10 [Note] InnoDB: 128 rollback segment(s) are active.
140826 14:21:10 [Note] InnoDB: Waiting for purge to start
140826 14:21:10 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.17-65.0 started; log sequence number 1616704
140826 14:21:11 [Note] InnoDB: FTS optimize thread exiting.
140826 14:21:11 [Note] InnoDB: Starting shutdown...
140826 14:21:13 [Note] InnoDB: Shutdown completed; log sequence number 1616714
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h 24d6adf7503e password 'new-password'
Alternatively you can run:
'./bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='./data'
You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from
SkySQL Ab. You can contact us about this at sales@skysql.com.
Alternatively consider joining our community based development effort:
http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
---> 783ee8a4ed80
Removing intermediate container a30888252206
Step 9 : RUN chown -R root /usr/local/mysql
---> Running in 3e33b22b45b9
---> e58e4f08e82a
Removing intermediate container 3e33b22b45b9
Step 10 : RUN chown -R mysql /usr/local/mysql/data
---> Running in 7ff915c93d6f
---> bef48e9f769e
Removing intermediate container 7ff915c93d6f
Step 11 : RUN cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
---> Running in ea249def0eca
---> ce96d94a5e58
Removing intermediate container ea249def0eca
Step 12 : RUN chmod +x /etc/init.d/mysql
---> Running in 1f74fe9dc89c
---> 7a89c7b8c155
Removing intermediate container 1f74fe9dc89c
Step 13 : RUN echo "PATH=$PATH:/usr/local/mysql/mysql/bin" >> /etc/profile.d/mysql.sh
---> Running in a4a3c33c2c7d
---> cb746a9ae90f
Removing intermediate container a4a3c33c2c7d
Successfully built cb746a9ae90f #构建成功
3、查看所有可用镜像
docker images
输出信息:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mariadb10.0.12 latest cb746a9ae90f About a minute ago 2.182 GB
centos centos6 b1bd49907d55 3 weeks ago 212.5 MB
centos centos7 b157b77b1a65 3 weeks ago 243.7 MB
centos latest b157b77b1a65 3 weeks ago 243.7 MB
4、运行容器,使用刚才构建的镜像
docker run -t -i 4a2a4d05f753 bash
bash-4.1# /etc/init.d/mysql start #启动mysql服务
Starting MySQL... SUCCESS!
bash-4.1# netstat -ntlp #查看进程监听
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
bash-4.1# . /etc/profile
bash-4.1# mysql #验证mysql是否可访问
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.0.12-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
5、查看所有容器,包括没有运行的,不加-a将只显示运行的容器
docker ps -a
输出信息:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a2a4d05f753 mariadb10.0.12:latest bash 7 minutes ago Exited (0) 3 seconds ago agitated_pike8
6、停止运行的容器
docker stop 4a2a4d05f753
7、删除容器
# docker rm 4a2a4d05f753
4a2a4d05f753
8、删除镜像,需要先删除运行在镜像上的容器
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mariadb10.0.12 latest 4a2a4d05f753 9 minutes ago 2.182 GB
centos centos6 b1bd49907d55 3 weeks ago 212.5 MB
centos centos7 b157b77b1a65 3 weeks ago 243.7 MB
centos latest b157b77b1a65 3 weeks ago 243.7 MB
运行docker rmi删除镜像:
# docker rmi 4a2a4d05f753
Untagged: mariadb10.0.12:latest
Deleted: cb746a9ae90f2976ba1a834313e586cfa9365d4a8cd918f8742772ea84d3d8d1
Deleted: 7a89c7b8c155ba2b4beedacafecf72cfc1c838a0315d698acd3591cf19fcafd7
Deleted: ce96d94a5e5873032a345c69eff521e5a10322bc3337c91df002ebce61cd63d9
Deleted: bef48e9f769e4413267d9239a3b0543db06fee6a845c51f643e3b1aca9b1e058
Deleted: e58e4f08e82a93e43a1f44c37431b983a4bb12ee3e678a6862db0946ed7e8102
Deleted: 783ee8a4ed80ae59e9959cec2eadf968e685d5bb41986578b59973b0ea2514a4
Deleted: a40435ceb9908a452c456914ac0f1e84a0fc64c6770ac89d39bdd4e2e9a0d51e
Deleted: cfd75d85ecab2eea45da81100a5caa5b4f99b885191973d5e60316565dc6f654
Deleted: b53b056c84ebd29aee83f031e186cd89cb84e55c0cddde2222290f6bee7c8516
Deleted: 390c8679d083d65717dedcca344e1451153b75138c0475b5f75efc8867e4ef9e
Deleted: f716c7598e826afa7f5462e2ca8e1049a02b936d1d167a02e362b14df3909c89
Deleted: aa5cd41ad1cf3a3a9fdd1850e41cae570a6ddcf5a8c42e87bc84cd59188b4ec0
Deleted: 4b5749e211d01d20cd15f8e2e45f511cd3624045c8e2d1f4313cbc66c4b5535c
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos centos6 b1bd49907d55 3 weeks ago 212.5 MB
centos centos7 b157b77b1a65 3 weeks ago 243.7 MB
centos latest b157b77b1a65 3 weeks ago 243.7 MB
参考:
2、"Using Percona Server 5.6 with the Docker open-source engine":
http://www.mysqlperformanceblog.com/2014/05/13/using-percona-server-5-6-with-the-docker-open-source-engine/